View Javadoc

1   /*
2    * $Id: Set.java 516197 2007-03-08 22:32:20Z rgielen $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * &lt;s:set name="personName" value="person.name"/&gt;
75   * Hello, &lt;s:property value="#personName"/&gt;. 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 }