FileDocCategorySizeDatePackage
Set.javaAPI DocExample5683Mon Jul 23 13:26:36 BST 2007org.apache.struts2.components

Set

public class Set extends Component

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 complex expression and then simply reference that variable each time rather than the complex expression. This is useful in both cases: when the complex expression takes time (performance improvement) or is hard to read (code readability improvement).

If the tag is used with body content, the evaluation of the value parameter is omitted. Instead, the String to which the body eveluates is set as value for the scoped variable.

The scopes available are as follows :-
  • application - the value will be set in application scope according to servlet spec. using the name as its key
  • session - the value will be set in session scope according to servlet spec. using the name as key
  • request - the value will be set in request scope according to servlet spec. using the name as key
  • page - the value will be set in request scope according to servlet sepc. using the name as key
  • action - the value will be set in the request scope and Struts' action context using the name as key
NOTE:

If no scope is specified, it will default to action scope.

Parameters

  • name* (String): The name of the new variable that is assigned the value of value
  • value (Object): The value that is assigned to the variable named name
  • scope (String): The scope in which to assign the variable. Can be application, session, request, page, or action. By default it is action.

Examples


<s:set name="personName" value="person.name"/>
Hello, <s:property value="#personName"/>. How are you?

Fields Summary
protected String
name
protected String
scope
protected String
value
Constructors Summary
public Set(com.opensymphony.xwork2.util.ValueStack stack)

        super(stack);
    
Methods Summary
public booleanend(java.io.Writer writer, java.lang.String body)

        ValueStack stack = getStack();

        Object o;
        if (value == null) {
        	if (body!=null && !body.equals("")) {
        		o = body;
        	} else {
        		o = findValue("top");
        	}
        } else {
        	o = findValue(value);
        }
        
        body="";

        String name;
        if (altSyntax()) {
            name = findString(this.name, "name", "Name is required");
        } else {
            name = this.name;

            if (this.name == null) {
                throw fieldError("name", "Name is required", null);
            }
        }

        if ("application".equalsIgnoreCase(scope)) {
            stack.setValue("#application['" + name + "']", o);
        } else if ("session".equalsIgnoreCase(scope)) {
            stack.setValue("#session['" + name + "']", o);
        } else if ("request".equalsIgnoreCase(scope)) {
            stack.setValue("#request['" + name + "']", o);
        } else if ("page".equalsIgnoreCase(scope)) {
            stack.setValue("#attr['" + name + "']", o, false);
        } else {
            stack.getContext().put(name, o);
            stack.setValue("#attr['" + name + "']", o, false);
        }

        return super.end(writer, body);
    
public voidsetName(java.lang.String name)

        this.name = name;
    
public voidsetScope(java.lang.String scope)

        this.scope = scope;
    
public voidsetValue(java.lang.String value)

        this.value = value;