View Javadoc

    1   /*
    2    * $Id: Set.java 497654 2007-01-19 00:21:57Z 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   *
    37   * The scopes available are as follows :-
    38   * <ul>
    39   *   <li>application - the value will be set in application scope according to servlet spec. using the name as its key</li>
    40   *   <li>session - the value will be set in session scope according to servlet spec. using the name as key </li>
    41   *   <li>request - the value will be set in request scope according to servlet spec. using the name as key </li>
    42   *   <li>page - the value will be set in request scope according to servlet sepc. using the name as key</li>
    43   *   <li>action - the value will be set in the request scope and Struts' action context using the name as key</li>
    44   * </ul>
    45   *
    46   * NOTE:<p/>
    47   * If no scope is specified, it will default to action scope.
    48   *
    49   * <!-- END SNIPPET: javadoc -->
    50   *
    51   * <p/> <b>Parameters</b>
    52   *
    53   * <!-- START SNIPPET: params -->
    54   *
    55   * <ul>
    56   *
    57   * <li>name* (String): The name of the new variable that is assigned the value of <i>value</i></li>
    58   *
    59   * <li>value (Object): The value that is assigned to the variable named <i>name</i></li>
    60   *
    61   * <li>scope (String): The scope in which to assign the variable. Can be <b>application</b>, <b>session</b>,
    62   * <b>request</b>, <b>page</b>, or <b>action</b>. By default it is <b>action</b>.</li>
    63   *
    64   * </ul>
    65   *
    66   * <!-- END SNIPPET: params -->
    67   *
    68   * <p/> <b>Examples</b>
    69   *
    70   * <pre>
    71   * <!-- START SNIPPET: example -->
    72   * &lt;s:set name="personName" value="person.name"/&gt;
    73   * Hello, &lt;s:property value="#personName"/&gt;. How are you?
    74   * <!-- END SNIPPET: example -->
    75   * </pre>
    76   *
    77   */
    78  @StrutsTag(name="set", tldBodyContent="empty", tldTagClass="org.apache.struts2.views.jsp.SetTag", description="Assigns a value to a variable in a specified scope")
    79  public class Set extends Component {
    80      protected String name;
    81      protected String scope;
    82      protected String value;
    83
    84      public Set(ValueStack stack) {
    85          super(stack);
    86      }
    87
    88      public boolean end(Writer writer, String body) {
    89          ValueStack stack = getStack();
    90
    91          if (value == null) {
    92              value = "top";
    93          }
    94
    95          Object o = findValue(value);
    96
    97          String name;
    98          if (altSyntax()) {
    99              name = findString(this.name, "name", "Name is required");
    100         } else {
    101             name = this.name;
    102
    103             if (this.name == null) {
    104                 throw fieldError("name", "Name is required", null);
    105             }
    106         }
    107
    108         if ("application".equalsIgnoreCase(scope)) {
    109             stack.setValue("#application['" + name + "']", o);
    110         } else if ("session".equalsIgnoreCase(scope)) {
    111             stack.setValue("#session['" + name + "']", o);
    112         } else if ("request".equalsIgnoreCase(scope)) {
    113             stack.setValue("#request['" + name + "']", o);
    114         } else if ("page".equalsIgnoreCase(scope)) {
    115             stack.setValue("#attr['" + name + "']", o, false);
    116         } else {
    117             stack.getContext().put(name, o);
    118             stack.setValue("#attr['" + name + "']", o, false);
    119         }
    120
    121         return super.end(writer, body);
    122     }
    123
    124     @StrutsTagAttribute(description=" The name of the new variable that is assigned the value of <i>value</i>", required=true)
    125     public void setName(String name) {
    126         this.name = name;
    127     }
    128
    129     @StrutsTagAttribute(description="The scope in which to assign the variable. Can be <b>application</b>" +
    130                 ", <b>session</b>, <b>request</b>, <b>page</b>, or <b>action</b>.", defaultValue="action")
    131     public void setScope(String scope) {
    132         this.scope = scope;
    133     }
    134
    135     @StrutsTagAttribute(description="The value that is assigned to the variable named <i>name</i>")
    136     public void setValue(String value) {
    137         this.value = value;
    138     }
    139 }