1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.components;
22
23 import java.io.Writer;
24
25 import org.apache.struts2.views.annotations.StrutsTag;
26 import org.apache.struts2.views.annotations.StrutsTagAttribute;
27
28 import com.opensymphony.xwork2.util.ValueStack;
29
30 /***
31 * <!-- START SNIPPET: javadoc -->
32 * <p>Push value on stack for simplified usage.</p>
33 * <!-- END SNIPPET: javadoc -->
34 *
35 * <!-- START SNIPPET: params -->
36 * <ul>
37 * <li>value* (Object) - value to be pushed into the top of the stack</li>
38 * </ul>
39 * <!-- END SNIPPET: params -->
40 *
41 *
42 * <p/> <b>Examples</b>
43 * <pre>
44 * <!-- START SNIPPET: example1 -->
45 * <s:push value="user">
46 * <s:propery value="firstName" />
47 * <s:propery value="lastName" />
48 * </s:push>
49 * <!-- END SNIPPET: example1 -->
50 * </pre>
51 *
52 * <!-- START SNIPPET: example1description -->
53 * Pushed user into the stack, and hence property tag could access user's properties
54 * (firstName, lastName etc) since user is not at the top of the stack
55 * <!-- END SNIPPET: example1description -->
56 *
57 * <pre>
58 * <!-- START SNIPPET: example2 -->
59 * <s:push value="myObject"> ----- (1)
60 * <s:bean name="jp.SomeBean" id="myBean"/> ----- (2)
61 * <s:param name="myParam" value="top"/> ----- (3)
62 * </s:bean>
63 * </s:push>
64 * <!-- END SNIPPET: example2 -->
65 * </pre>
66 *
67 * <pre>
68 * <!-- START SNIPPET: example2description -->
69 * when in (1), myObject is at the top of the stack
70 * when in (2), jp.SomeBean is in the top of stack, also in stack's context with key myBean
71 * when in (3), top will get the jp.SomeBean instance
72 * <!-- END SNIPPET: example2description -->
73 * </pre>
74 *
75 * <pre>
76 * <!-- START SNIPPET: example3 -->
77 * <s:push value="myObject"> ---(A)
78 * <s:bean name="jp.SomeBean" id="myBean"/> ---(B)
79 * <s:param name="myParam" value="top.mySomeOtherValue"/> ---(C)
80 * </s:bean>
81 * </s:push>
82 * <!-- END SNIPPET: example3 -->
83 * </pre>
84 *
85 * <pre>
86 * <!-- START SNIPPET: example3description -->
87 * when in (A), myObject is at the top of the stack
88 * when in (B), jp.SomeBean is at the top of the stack, also in context with key myBean
89 * when in (C), top refers to jp.SomeBean instance. so top.mySomeOtherValue would invoke SomeBean's mySomeOtherValue() method
90 * <!-- END SNIPPET: example3description -->
91 * </pre>
92 *
93 * <pre>
94 * <!-- START SNIPPET: example4 -->
95 * <s:push value="myObject"> ---- (i)
96 * <s:bean name="jp.SomeBean" id="myBean"/> ---- (ii)
97 * <s:param name="myParam" value="[1].top"/> -----(iii)
98 * </s:bean>
99 * </s:push>
100 * <!-- END SNIPPET: example4 -->
101 * </pre>
102 *
103 * <pre>
104 * <!-- START SNIPPET: example4description -->
105 * when in (i), myObject is at the top of the stack
106 * when in (ii), jp.SomeBean is at the top of the stack, followed by myObject
107 * when in (iii), [1].top will returned top of the cut of stack starting from myObject, namely myObject itself
108 * <!-- END SNIPPET: example4description -->
109 * </pre>
110 *
111 */
112 @StrutsTag(name="push", tldTagClass="org.apache.struts2.views.jsp.PushTag", description="Push value on stack for simplified usage.")
113 public class Push extends Component {
114 protected String value;
115 protected boolean pushed;
116
117 public Push(ValueStack stack) {
118 super(stack);
119 }
120
121 public boolean start(Writer writer) {
122 boolean result = super.start(writer);
123
124 ValueStack stack = getStack();
125
126 if (stack != null) {
127 stack.push(findValue(value, "value", "You must specify a value to push on the stack. Example: person"));
128 pushed = true;
129 } else {
130 pushed = false;
131 }
132
133 return result;
134 }
135
136 public boolean end(Writer writer, String body) {
137 ValueStack stack = getStack();
138
139 if (pushed && (stack != null)) {
140 stack.pop();
141 }
142
143 return super.end(writer, body);
144 }
145
146 @StrutsTagAttribute(description="Value to push on stack", required=true)
147 public void setValue(String value) {
148 this.value = value;
149 }
150 }