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