View Javadoc

1   /*
2    * $Id: Set.java 454565 2006-10-10 00:02:56Z jmitchell $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * &lt;s:set name="personName" value="person.name"/&gt;
67   * Hello, &lt;s:property value="#personName"/&gt;. 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 }