1:Description

 

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 page 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.

即可以设置五个范围:application, session, request, page, action,每个范围有不同的含义

 

2:Parameters

   

Name       Required      Default       Evaluated    Type        description

    id                   false                                       false                  String           Deprecated. Use 'var' instead

    name              false                                        false                  String          Deprecated. Use 'var' instead

    scope              false                  action            false                  String          The scope in which to assign the variable. Can be application, session, request, page, or action.

 

    value            false                                         false                 String          The value that is assigned to the variable named name

    var               false                                          false                String           Name used to reference the value pushed into the Value Stack

 

3:例子

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


使用<s:set name="personName" value="person.name" />来定义一个personName的变量,该变量的值为person.name属性的值,


并用<s:property value="#personName"/> 来访问该变量的值


<li>bean, 定义bean,并使用param来设定新的属性值:
         <s:bean name="com.capinfotech.ognl.action.Dog">
            <s:param name="name" value="dongdong" />
         </s:bean>
     </li>
     <li>
          <s:bean name="com.capinfotech.ognl.action.Dog" var="myDog">
             <s:param name="name" value="'mydog'"></s:param>
          </s:bean>
     </li>

第一个<li>里的例子定义一个Dog的对象,该对象的name属性的值为dongdong在ActionContext里对应的值

第二个<li>里的例子定义一个Dog的对象,该对象的name属性的值为mydog

注意:<s:param>里的value的类型为string类型的