001: package com.opensymphony.webwork.components;
002:
003: import com.opensymphony.xwork.util.OgnlValueStack;
004:
005: import java.io.Writer;
006:
007: /**
008: * <!-- START SNIPPET: javadoc -->
009: * <p>Push value on stack for simplified usage.</p>
010: * <!-- END SNIPPET: javadoc -->
011: *
012: * <!-- START SNIPPET: params -->
013: * <ul>
014: * <li>value* (Object) - value to be pushed into the top of the stack</li>
015: * </ul>
016: * <!-- END SNIPPET: params -->
017: *
018: *
019: * <p/> <b>Examples</b>
020: * <pre>
021: * <!-- START SNIPPET: example1 -->
022: * <ww:push value="user">
023: * <ww:propery value="firstName" />
024: * <ww:propery value="lastName" />
025: * </ww:push>
026: * <!-- END SNIPPET: example1 -->
027: * </pre>
028: *
029: * <!-- START SNIPPET: example1description -->
030: * Pushed user into the stack, and hence property tag could access user's properties
031: * (firstName, lastName etc) since user is not at the top of the stack
032: * <!-- END SNIPPET: example1description -->
033: *
034: * <pre>
035: * <!-- START SNIPPET: example2 -->
036: * <ww:push value="myObject"> ----- (1)
037: * <ww:bean name="jp.SomeBean" id="myBean"/> ----- (2)
038: * <ww:param name="myParam" value="top"/> ----- (3)
039: * </ww:bean>
040: * </ww:push>
041: * <!-- END SNIPPET: example2 -->
042: * </pre>
043: *
044: * <pre>
045: * <!-- START SNIPPET: example2description -->
046: * when in (1), myObject is at the top of the stack
047: * when in (2), jp.SomeBean is in the top of stack, also in stack's context with key myBean
048: * when in (3), top will get the jp.SomeBean instance
049: * <!-- END SNIPPET: example2description -->
050: * </pre>
051: *
052: * <pre>
053: * <!-- START SNIPPET: example3 -->
054: * <ww:push value="myObject"> ---(A)
055: * <ww:bean name="jp.SomeBean" id="myBean"/> ---(B)
056: * <ww:param name="myParam" value="top.mySomeOtherValue"/> ---(C)
057: * </ww:bean>
058: * </ww:push>
059: * <!-- END SNIPPET: example3 -->
060: * </pre>
061: *
062: * <pre>
063: * <!-- START SNIPPET: example3description -->
064: * when in (A), myObject is at the top of the stack
065: * when in (B), jp.SomeBean is at the top of the stack, also in context with key myBean
066: * when in (C), top refers to jp.SomeBean instance. so top.mySomeOtherValue would invoke SomeBean's mySomeOtherValue() method
067: * <!-- END SNIPPET: example3description -->
068: * </pre>
069: *
070: * <pre>
071: * <!-- START SNIPPET: example4 -->
072: * <ww:push value="myObject"> ---- (i)
073: * <ww:bean name="jp.SomeBean" id="myBean"/> ---- (ii)
074: * <ww:param name="myParam" value="[1].top"/> -----(iii)
075: * </ww:bean>
076: * </ww:push>
077: * <!-- END SNIPPET: example4 -->
078: * </pre>
079: *
080: * <pre>
081: * <!-- START SNIPPET: example4description -->
082: * when in (i), myObject is at the top of the stack
083: * when in (ii), jp.SomeBean is at the top of the stack, followed by myObject
084: * when in (iii), [1].top will returned top of the cut of stack starting from myObject, namely myObject itself
085: * <!-- END SNIPPET: example4description -->
086: * </pre>
087: *
088: *
089: * @author Patrick Lightbody
090: * @author Rene Gielen
091: * @author tm_jee
092: * @version $Revision: 2468 $
093: * @since 2.2
094: *
095: * @ww.tag name="push" tld-body-content="JSP" tld-tag-class="com.opensymphony.webwork.views.jsp.PushTag"
096: * description="Push value on stack for simplified usage."
097: */
098: public class Push extends Component {
099: protected String value;
100: protected boolean pushed;
101:
102: public Push(OgnlValueStack stack) {
103: super (stack);
104: }
105:
106: public boolean start(Writer writer) {
107: boolean result = super .start(writer);
108:
109: OgnlValueStack stack = getStack();
110:
111: if (stack != null) {
112: stack
113: .push(findValue(value, "value",
114: "You must specify a value to push on the stack. Example: person"));
115: pushed = true;
116: } else {
117: pushed = false; // need to ensure push is assigned, otherwise we may have a leftover value
118: }
119:
120: return result;
121: }
122:
123: public boolean end(Writer writer, String body) {
124: OgnlValueStack stack = getStack();
125:
126: if (pushed && (stack != null)) {
127: stack.pop();
128: }
129:
130: return super .end(writer, body);
131: }
132:
133: /**
134: * Value to push on stack
135: * @ww.tagattribute required="true"
136: */
137: public void setValue(String value) {
138: this.value = value;
139: }
140: }
|