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