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>The set tag assigns a value to a variable in a specified scope. It is useful when you wish to assign a variable to a
010: * complex expression and then simply reference that variable each time rather than the complex expression. This is
011: * useful in both cases: when the complex expression takes time (performance improvement) or is hard to read (code
012: * readability improvement).</P>
013: *
014: * <p>The set tag accepts body. However the followings need to be take note when using set tag with body</p>
015: * <ul>
016: * <li>body are treated as String and will not be parsed by OGNL</li>
017: * <li>body could be scriptlet or JSP tags, the String representation of scriptlet or JSP tags will be used</li>
018: * <li>A non-empty will take precedence if there's also a value attribute present, if the body is empty, then the value attribute will be used</li>
019: * </ul>
020: *
021: * <!-- END SNIPPET: javadoc -->
022: *
023: * <p/> <b>Parameters</b>
024: *
025: * <!-- START SNIPPET: params -->
026: *
027: * <ul>
028: *
029: * <li>name* (String): The name of the new variable that is assigned the value of <i>value</i></li>
030: *
031: * <li>value (Object): The value that is assigned to the variable named <i>name</i></li>
032: *
033: * <li>scope (String): The scope in which to assign the variable. Can be <b>application</b>, <b>session</b>,
034: * <b>request</b>, <b>page</b>, or <b>action</b>. By default it is <b>action</b>.</li>
035: *
036: * </ul>
037: *
038: * <!-- END SNIPPET: params -->
039: *
040: * <p/> <b>Examples</b>
041: *
042: * <pre>
043: * <!-- START SNIPPET: example -->
044: * <ww:set name="personName" value="person.name"/>
045: * Hello, <ww:property value="%{#personName}"/>. How are you?
046: *
047: * <ww:set name="personName">
048: * <ww:property value="%{'some string'}" />
049: * </ww:set>
050: * Hello, <ww:property value="%{#personName}"/>. How are you?
051: *
052: * <ww:set name="personName">
053: * <c:set value="${person.name}" />
054: * </ww:set>
055: * Hello, <ww:property value="%{#personName}"/>. How are you?
056: * <!-- END SNIPPET: example -->
057: * </pre>
058: *
059: * @author Patrick Lightbody
060: * @author Rene Gielen
061: * @version $Revision: 2871 $
062: * @since 2.2
063: *
064: * @ww.tag name="set" tld-body-content="JSP" tld-tag-class="com.opensymphony.webwork.views.jsp.SetTag"
065: * description="Assigns a value to a variable in a specified scope"
066: */
067: public class Set extends Component {
068: protected String name;
069: protected String scope;
070: protected String value;
071:
072: public Set(OgnlValueStack stack) {
073: super (stack);
074: }
075:
076: /**
077: * Returns true, so that we use EVAL_BODY_BUFFERED instead of EVAL_BODY_INCLUDE
078: * @see com.opensymphony.webwork.components.Component#usesBody()
079: */
080: public boolean usesBody() {
081: return true;
082: }
083:
084: public boolean end(Writer writer, String body) {
085: OgnlValueStack stack = getStack();
086:
087: Object o = null;
088: if (body != null && body.trim().length() > 0) {
089: o = body;
090: body = ""; // empty the body, we don't want it to be writen out.
091: } else {
092: if (value == null) {
093: value = "top";
094: }
095: o = findValue(value);
096: }
097:
098: String name;
099: if (altSyntax()) {
100: name = findString(this .name, "name", "Name is required");
101: } else {
102: name = this .name;
103:
104: if (this .name == null) {
105: throw fieldError("name", "Name is required", null);
106: }
107: }
108:
109: if ("application".equalsIgnoreCase(scope)) {
110: stack.setValue("#application['" + name + "']", o);
111: } else if ("session".equalsIgnoreCase(scope)) {
112: stack.setValue("#session['" + name + "']", o);
113: } else if ("request".equalsIgnoreCase(scope)) {
114: stack.setValue("#request['" + name + "']", o);
115: } else if ("page".equalsIgnoreCase(scope)) {
116: stack.setValue("#attr['" + name + "']", o, false);
117: }
118: //WW-796: SetTag "scope" attribute do not put the object to stack if the scope is given.
119: stack.getContext().put(name, o);
120:
121: return super .end(writer, body);
122: }
123:
124: /**
125: * The name of the new variable that is assigned the value of <i>value</i>
126: * @ww.tagattribute required="true" type="String"
127: */
128: public void setName(String name) {
129: this .name = name;
130: }
131:
132: /**
133: * The scope in which to assign the variable. Can be <b>application</b>, <b>session</b>, <b>request</b>, <b>page</b>, or <b>action</b>.
134: * @ww.tagattribute required="false" type="String" default="action"
135: */
136: public void setScope(String scope) {
137: this .scope = scope;
138: }
139:
140: /**
141: * The value that is assigned to the variable named <i>name</i>
142: * @ww.tagattribute required="false"
143: */
144: public void setValue(String value) {
145: this.value = value;
146: }
147: }
|