1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.components;
19
20 import java.io.Writer;
21
22 import com.opensymphony.xwork2.util.ValueStack;
23
24 /***
25 * <!-- START SNIPPET: javadoc -->
26 * <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
27 * complex expression and then simply reference that variable each time rather than the complex expression. This is
28 * useful in both cases: when the complex expression takes time (performance improvement) or is hard to read (code
29 * readability improvement).</P>
30 *
31 * The scopes available are as follows :-
32 * <ul>
33 * <li>application - the value will be set in application scope according to servlet spec. using the name as its key</li>
34 * <li>session - the value will be set in session scope according to servlet spec. using the name as key </li>
35 * <li>request - the value will be set in request scope according to servlet spec. using the name as key </li>
36 * <li>page - the value will be set in request scope according to servlet sepc. using the name as key</li>
37 * <li>action - the value will be set in the request scope and Struts' action context using the name as key</li>
38 * </ul>
39 *
40 * NOTE:<p/>
41 * If no scope is specified, it will default to action scope.
42 *
43 * <!-- END SNIPPET: javadoc -->
44 *
45 * <p/> <b>Parameters</b>
46 *
47 * <!-- START SNIPPET: params -->
48 *
49 * <ul>
50 *
51 * <li>name* (String): The name of the new variable that is assigned the value of <i>value</i></li>
52 *
53 * <li>value (Object): The value that is assigned to the variable named <i>name</i></li>
54 *
55 * <li>scope (String): The scope in which to assign the variable. Can be <b>application</b>, <b>session</b>,
56 * <b>request</b>, <b>page</b>, or <b>action</b>. By default it is <b>action</b>.</li>
57 *
58 * </ul>
59 *
60 * <!-- END SNIPPET: params -->
61 *
62 * <p/> <b>Examples</b>
63 *
64 * <pre>
65 * <!-- START SNIPPET: example -->
66 * <s:set name="personName" value="person.name"/>
67 * Hello, <s:property value="#personName"/>. How are you?
68 * <!-- END SNIPPET: example -->
69 * </pre>
70 *
71 * @s.tag name="set" tld-body-content="empty" tld-tag-class="org.apache.struts2.views.jsp.SetTag"
72 * description="Assigns a value to a variable in a specified scope"
73 */
74 public class Set extends Component {
75 protected String name;
76 protected String scope;
77 protected String value;
78
79 public Set(ValueStack stack) {
80 super(stack);
81 }
82
83 public boolean end(Writer writer, String body) {
84 ValueStack stack = getStack();
85
86 if (value == null) {
87 value = "top";
88 }
89
90 Object o = findValue(value);
91
92 String name;
93 if (altSyntax()) {
94 name = findString(this.name, "name", "Name is required");
95 } else {
96 name = this.name;
97
98 if (this.name == null) {
99 throw fieldError("name", "Name is required", null);
100 }
101 }
102
103 if ("application".equalsIgnoreCase(scope)) {
104 stack.setValue("#application['" + name + "']", o);
105 } else if ("session".equalsIgnoreCase(scope)) {
106 stack.setValue("#session['" + name + "']", o);
107 } else if ("request".equalsIgnoreCase(scope)) {
108 stack.setValue("#request['" + name + "']", o);
109 } else if ("page".equalsIgnoreCase(scope)) {
110 stack.setValue("#attr['" + name + "']", o, false);
111 } else {
112 stack.getContext().put(name, o);
113 stack.setValue("#attr['" + name + "']", o, false);
114 }
115
116 return super.end(writer, body);
117 }
118
119 /***
120 * The name of the new variable that is assigned the value of <i>value</i>
121 * @s.tagattribute required="true" type="String"
122 */
123 public void setName(String name) {
124 this.name = name;
125 }
126
127 /***
128 * 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>.
129 * @s.tagattribute required="false" type="String" default="action"
130 */
131 public void setScope(String scope) {
132 this.scope = scope;
133 }
134
135 /***
136 * The value that is assigned to the variable named <i>name</i>
137 * @s.tagattribute required="false"
138 */
139 public void setValue(String value) {
140 this.value = value;
141 }
142 }