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>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
34 * complex expression and then simply reference that variable each time rather than the complex expression. This is
35 * useful in both cases: when the complex expression takes time (performance improvement) or is hard to read (code
36 * readability improvement).</p>
37 * <p>If the tag is used with body content, the evaluation of the value parameter is omitted. Instead, the String to
38 * which the body eveluates is set as value for the scoped variable.</p>
39 *
40 * The scopes available are as follows :-
41 * <ul>
42 * <li>application - the value will be set in application scope according to servlet spec. using the name as its key</li>
43 * <li>session - the value will be set in session scope according to servlet spec. using the name as key </li>
44 * <li>request - the value will be set in request scope according to servlet spec. using the name as key </li>
45 * <li>page - the value will be set in page scope according to servlet sepc. using the name as key</li>
46 * <li>action - the value will be set in the request scope and Struts' action context using the name as key</li>
47 * </ul>
48 *
49 * NOTE:<p/>
50 * If no scope is specified, it will default to action scope.
51 *
52 * <!-- END SNIPPET: javadoc -->
53 *
54 * <p/> <b>Parameters</b>
55 *
56 * <!-- START SNIPPET: params -->
57 *
58 * <ul>
59 *
60 * <li>name* (String): The name of the new variable that is assigned the value of <i>value</i></li>
61 *
62 * <li>value (Object): The value that is assigned to the variable named <i>name</i></li>
63 *
64 * <li>scope (String): The scope in which to assign the variable. Can be <b>application</b>, <b>session</b>,
65 * <b>request</b>, <b>page</b>, or <b>action</b>. By default it is <b>action</b>.</li>
66 *
67 * </ul>
68 *
69 * <!-- END SNIPPET: params -->
70 *
71 * <p/> <b>Examples</b>
72 *
73 * <pre>
74 * <!-- START SNIPPET: example -->
75 * <s:set name="personName" value="person.name"/>
76 * Hello, <s:property value="#personName"/>. How are you?
77 * <!-- END SNIPPET: example -->
78 * </pre>
79 *
80 */
81 @StrutsTag(name="set", tldBodyContent="JSP", tldTagClass="org.apache.struts2.views.jsp.SetTag", description="Assigns a value to a variable in a specified scope")
82 public class Set extends ContextBean {
83 protected String scope;
84 protected String value;
85
86 public Set(ValueStack stack) {
87 super(stack);
88 }
89
90 public boolean end(Writer writer, String body) {
91 ValueStack stack = getStack();
92
93 Object o;
94 if (value == null) {
95 if (body != null && !body.equals("")) {
96 o = body;
97 } else {
98 o = findValue("top");
99 }
100 } else {
101 o = findValue(value);
102 }
103
104 body="";
105
106 if ("application".equalsIgnoreCase(scope)) {
107 stack.setValue("#application['" + getVar() + "']", o);
108 } else if ("session".equalsIgnoreCase(scope)) {
109 stack.setValue("#session['" + getVar() + "']", o);
110 } else if ("request".equalsIgnoreCase(scope)) {
111 stack.setValue("#request['" + getVar() + "']", o);
112 } else if ("page".equalsIgnoreCase(scope)) {
113 stack.setValue("#attr['" + getVar() + "']", o, false);
114 } else {
115 stack.getContext().put(getVar(), o);
116 stack.setValue("#attr['" + getVar() + "']", o, false);
117 }
118
119 return super.end(writer, body);
120 }
121
122
123
124
125 @StrutsTagAttribute(description="Name used to reference the value pushed into the Value Stack")
126 public void setVar(String var) {
127 super.setVar(var);
128 }
129
130 @StrutsTagAttribute(description="Deprecated. Use 'var' instead")
131 public void setName(String name) {
132 setVar(name);
133 }
134
135 @StrutsTagAttribute(description="The scope in which to assign the variable. Can be <b>application</b>" +
136 ", <b>session</b>, <b>request</b>, <b>page</b>, or <b>action</b>.", defaultValue="action")
137 public void setScope(String scope) {
138 this.scope = scope;
139 }
140
141 @StrutsTagAttribute(description="The value that is assigned to the variable named <i>name</i>")
142 public void setValue(String value) {
143 this.value = value;
144 }
145
146 @Override
147 public boolean usesBody() {
148 return true;
149 }
150 }