001: /*
002: * $Id: Set.java 516197 2007-03-08 22:32:20Z rgielen $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.components;
022:
023: import java.io.Writer;
024:
025: import org.apache.struts2.views.annotations.StrutsTag;
026: import org.apache.struts2.views.annotations.StrutsTagAttribute;
027:
028: import com.opensymphony.xwork2.util.ValueStack;
029:
030: /**
031: * <!-- START SNIPPET: javadoc -->
032: * <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
033: * complex expression and then simply reference that variable each time rather than the complex expression. This is
034: * useful in both cases: when the complex expression takes time (performance improvement) or is hard to read (code
035: * readability improvement).</p>
036: * <p>If the tag is used with body content, the evaluation of the value parameter is omitted. Instead, the String to
037: * which the body eveluates is set as value for the scoped variable.</p>
038: *
039: * The scopes available are as follows :-
040: * <ul>
041: * <li>application - the value will be set in application scope according to servlet spec. using the name as its key</li>
042: * <li>session - the value will be set in session scope according to servlet spec. using the name as key </li>
043: * <li>request - the value will be set in request scope according to servlet spec. using the name as key </li>
044: * <li>page - the value will be set in request scope according to servlet sepc. using the name as key</li>
045: * <li>action - the value will be set in the request scope and Struts' action context using the name as key</li>
046: * </ul>
047: *
048: * NOTE:<p/>
049: * If no scope is specified, it will default to action scope.
050: *
051: * <!-- END SNIPPET: javadoc -->
052: *
053: * <p/> <b>Parameters</b>
054: *
055: * <!-- START SNIPPET: params -->
056: *
057: * <ul>
058: *
059: * <li>name* (String): The name of the new variable that is assigned the value of <i>value</i></li>
060: *
061: * <li>value (Object): The value that is assigned to the variable named <i>name</i></li>
062: *
063: * <li>scope (String): The scope in which to assign the variable. Can be <b>application</b>, <b>session</b>,
064: * <b>request</b>, <b>page</b>, or <b>action</b>. By default it is <b>action</b>.</li>
065: *
066: * </ul>
067: *
068: * <!-- END SNIPPET: params -->
069: *
070: * <p/> <b>Examples</b>
071: *
072: * <pre>
073: * <!-- START SNIPPET: example -->
074: * <s:set name="personName" value="person.name"/>
075: * Hello, <s:property value="#personName"/>. How are you?
076: * <!-- END SNIPPET: example -->
077: * </pre>
078: *
079: */
080: @StrutsTag(name="set",tldBodyContent="JSP",tldTagClass="org.apache.struts2.views.jsp.SetTag",description="Assigns a value to a variable in a specified scope")
081: public class Set extends Component {
082: protected String name;
083: protected String scope;
084: protected String value;
085:
086: public Set(ValueStack stack) {
087: super (stack);
088: }
089:
090: public boolean end(Writer writer, String body) {
091: ValueStack stack = getStack();
092:
093: Object o;
094: if (value == null) {
095: if (body != null && !body.equals("")) {
096: o = body;
097: } else {
098: o = findValue("top");
099: }
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 value",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 application" + ", session, request, page, or action.",defaultValue="action")
139: public void setScope(String scope) {
140: this .scope = scope;
141: }
142:
143: @StrutsTagAttribute(description="The value that is assigned to the variable named name")
144: public void setValue(String value) {
145: this.value = value;
146: }
147: }
|