Struts2---tags标签

对struts2标签的详细说明可参考struts-tags的官方文档。或者参考http://www.roseindia.net/struts/struts2/struts-2-tags.shtml或者/struts-2.3.16.1-docs/struts-2.3.16.1/docs/generic-tag-reference.html

下面是对常用struts2标签的示例及简单说明,struts2标签主要可以分为generic tags(包含flow control tags和data tags), UI tags(Form tags 和non-Form tags).

具体如下:

The main difference between Struts Generic Tags and Struts UI Tags are:

  • The generic tags simply output some content directly from the tag while the UI tags uses templates and often group the output together with theme
下面分别对这些标签中主要使用的一些标签进行示例说明。

property标签。

propery标签可以设置的相关属性

Name
Required
Default
Evaluated
Type
Description
default false   false String The default value to be used if value attribute is null
escape false true false Boolean Deprecated. Use 'escapeHtml'. Whether to escape HTML
escapeCsv false false false Boolean Whether to escape CSV (useful to escape a value for a column)
escapeHtml false true false Boolean Whether to escape HTML
escapeJavaScript false false false Boolean Whether to escape Javascript
escapeXml false false false Boolean Whether to escape XML
value false <top of stack> false Object Value to be displayed

Examples

< s:push value="myBean">
     <!-- Example 1: -->
     < s:property value="myBeanProperty" />
 
     <!-- Example 2: --> TextUtils
     < s:property value="myBeanProperty" default="a default value" />
<!-- Example 3: -->
<li>property 设定HTML: <s:property value="'<hr/>'" escape="false"/> </li>
</ s:push >
Example 1 prints the result of myBean's getMyBeanProperty() method.
Example 2 prints the result of myBean's getMyBeanProperty() method and if it is null, print 'a default value' instead.
Example 3 要是escape为fasle,<hr />将会解析成html语言的换行符,否则将作为一纯字符串显示
有一点值得注意的是,如下这个例子:
<li>property 取值为字符串: <s:property value="'username'"/> </li>
   要是value指定的值为一个""括起来的,那么“”内的内容将被解析为ongl表达式,它将会去值栈中获取内容,要是value="'username'",这是指""内的内容仅仅是一个纯字符串,即username.struts2不会将其当做成ongl表达式来解析。

set标签

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

Parameters

Dynamic Attributes Allowed:
false
 
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 applicationsessionrequestpage, 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

Examples

< s:set var="personName" value="person.name"/>
Hello, < s:property value="#personName"/>
 
< s:set var="janesName">Jane Doe</ s:set >
< s:property value="#janesName"/>
如上所述,set标签常用在两种情况下:一个是某个标量名太复杂并多次使用了,可以用一个简单的变量进行替代,这样可以提高访问效率。还有一个就是某个变量名不好理解,可以使用一个更好理解的变量来关联此变量。set标签指定的变量名内容是一致的,类似c语言中的引用。
scope属性指定的是将此变量存放在哪两个容器中,默认放在action中(同时在,request域中也会有一份)。
<li>set 设定adminName值(默认为request 和 ActionContext): <s:set var="adminName" value="username" /></li>
		
		<li>set 从request取值: <s:property value="#request.adminName" /></li>
		<li>set 从ActionContext取值: <s:property value="#adminName" /></li>
		
		<%--<li>set 设定范围: <s:set name="adminPassword" value="password" scope="page"/></li>
		<li>set 从相应范围取值: <%=pageContext.getAttribute("adminPassword") %></li>
		--%>
		<li>set 设定var,范围为ActionContext: <s:set var="adminPassword" value="password" scope="session"/></li>
		<li>set 使用#取值: <s:property value="#adminPassword"/> </li>
		<li>set 从相应范围取值: <s:property value="#session.adminPassword"/> </li>
当使用set标签给某个变量指定替代变量后(使用var),这个var指定的变量将被放在context action中对应的scope指定的域中(默认直接存在此action context中,如上面的adminName),可以使用#来获取。如上面的adminPassword。
url:http://localhost:8080/Struts2_004/action/tags?username=zhangsan&password=1234
结果:
  • set 设定adminName值(默认为request 和 ActionContext):
  • set 从request取值: zhangsan
  • set 从ActionContext取值: zhangsan
  • set 设定var,范围为ActionContext:
  • set 使用#取值:
  • set 从相应范围取值: 1234

bean标签

Description

Instantiates a class that conforms to the JavaBeans specification. This tag has a body which can contain a number of Param elements to set any mutator methods on that class.

If the var attribute is set on the BeanTag, it will place the instantiated bean into the stack's Context.

Parameters

Dynamic Attributes Allowed:
false
 
Name
Required
Default
Evaluated
Type
Description
id false   false String Deprecated. Use 'var' instead
name true   false String The class name of the bean to be instantiated (must respect JavaBean specification)
var false   false String Name used to reference the value pushed into the Value Stack

Examples

<-- in freemarker form -->
[@s.bean name="org.apache.struts2.example.counter.SimpleCounter" var="counter"]
   [s:param name="foo" value="BAR"/]
   The value of foo is : [s:property value="foo"/], when inside the bean tag.< br />
[/s:bean]
 
<-- in jsp form -->
< s:bean name="org.apache.struts2.example.counter.SimpleCounter" var="counter">
   < s:param name="foo" value="BAR" />
   The value of foot is : < s:property value="foo"/>, when inside the bean tag < br />
</ s:bean >

This example instantiates a bean called SimpleCounter and sets the foo property (setFoo('BAR')). The SimpleCounter object is then pushed onto the Valuestack, which means that we can call its accessor methods (getFoo()) with the Property tag and get their values.

In the above example, the id has been set to a value of counter. This means that the SimpleCounter class will be placed into the stack's context. You can access the SimpleCounter class using a Struts tag:

<-- jsp form -->
<s:property value="#counter" />

<-- freemarker form --> [s:property value="#counter.foo"/]

In the property tag example, the # tells Ognl to search the context for the SimpleCounter class which has an id(key) of counter

<li>bean 定义bean,并使用param来设定新的属性值:
			<s:bean name="cn.itcast.domain.Dog" >
			<s:param name="name" value="'pp'"></s:param>
			  	
				<s:property value="name"/>				
			</s:bean>
		</li>
		
		<li>bean 查看debug情况:
			<s:bean name="cn.itcast.domain.Dog" var="myDog">
			  	<s:param name="name" value="'oudy'"></s:param>
			</s:bean>
			拿出值:
			<s:property value="#myDog.name"/>	
		</li>
上面的两个例子中,例子一中没有指定var(指定后,会在context action中存入一个名为var指定的名称的元素,值为name指定类的一个对象实例),它将会将这个类的实例对象作为一个元素存入到值栈中,名为类名,值为此类对象对应的相应属性。如下是在bean标签内显示的值栈(cn.itcast.domain.Dog为此bean标签生成的一个实例),不过此栈在bean标签外,此bean对应的对象实例将从值栈中移除:

Value Stack Contents

ObjectProperty NameProperty Value
cn.itcast.domain.Dognamepp
cn.itcast.action.TagsActiontextsnull
usernamezhangsan
actionErrors[]
errors{}
fieldErrors{}
errorMessages[]
containerThere is no read method for container
localezh_CN
actionMessages[]
useruser8
passwordnull
com.opensymphony.xwork2.DefaultTextProvidertextsnull
第二个例子,它将会创建一个实例对象,并将此实例对象存入到context action中,因此可以通过#来获取此实例对象的相关属性和方法。bean标签中的param子标签用来为bean对象相应的属性设置相关值
<s:param name="name" value="'oudy'"></s:param>
name为属性,value为要更新的值,若要对value进行赋值,字符串需要在一对单引号''括起来。否则将被当做ongl表达式。上面两例的输出结果:
  • bean 定义bean,并使用param来设定新的属性值: pp
  • bean 查看debug情况: 拿出值: oudy
push标签

Description

Push value on stack for simplified usage.

Parameters

Dynamic Attributes Allowed:
false
 
Name
Required
Default
Evaluated
Type
Description
value true   false String Value to push on stack

Examples

< s:push value="user">
     < s:propery value="firstName" />
     < s:propery value="lastName" />
</ s:push >
Pushed user into the stack, and hence property tag could access user's properties
(firstName, lastName etc) since user is now at the top of the stack
< s:push value="myObject">                              ----- (1)
      < s:bean name="jp.SomeBean" var="myBean"/>        ----- (2)
         < s:param name="myParam" value="top"/>        ----- (3)
      </ s:bean >
  </ s:push >
when in (1), myObject is at the top of the stack
when in (2), jp.SomeBean is in the top of stack, also in stack's context with key myBean
when in (3), top will get the jp.SomeBean instance
< s:push value="myObject">                                       ---(A)
    < s:bean name="jp.SomeBean" var="myBean"/>                   ---(B)
       < s:param name="myParam" value="top.mySomeOtherValue"/>  ---(C)
    </ s:bean >
</ s:push >
when in (A), myObject is at the top of the stack
when in (B), jp.SomeBean is at the top of the stack, also in context with key myBean
when in (C), top refers to jp.SomeBean instance. so top.mySomeOtherValue would invoke SomeBean's mySomeOtherValue() method
< s:push value="myObject">                                 ---- (i)
    < s:bean name="jp.SomeBean" var="myBean"/>             ---- (ii)
       < s:param name="myParam" value="[1].top"/>         -----(iii)
    </ s:bean >
</ s:push >
when in (i), myObject is at the top of the stack
when in (ii), jp.SomeBean is at the top of the stack, followed by myObject
when in (iii), [1].top will returned top of the cut of stack starting from myObject, namely myObject itself
push将会将一些元素放到栈顶。如例一中,在push中可以用property标签来访问其相关属性值。
include标签

Description

Include a servlet's output (result of servlet or a JSP page).

Note: Any additional params supplied to the included page are not accessible within the rendered page through the <s:property...> tag since no valuestack will be created. You can, however, access them in a servlet via the HttpServletRequest object or from a JSP page via a scriptlet.

How To access parameters

Icon

Parameters are passed as request parameters, so use the ${param.ParamName} notation to access them. Do not use the property tag to access parameters in included files.

Parameters

Dynamic Attributes Allowed:
false
 
Name
Required
Default
Evaluated
Type
Description
value true   false String The jsp/servlet output to include

Example

<-- One: -->
<s:include value="myJsp.jsp" />
 
<-- Two: -->
<s:include value="myJsp.jsp">
    <s:param name="param1" value="value2" />
    <s:param name="param2" value="value2" />
</s:include>
 
<-- Three: -->
<s:include value="myJsp.jsp">
    <s:param name="param1">value1</s:param>
    <s:param name="param2">value2</s:param>
</s:include>
Example one - do an include myJsp.jsp page
Example two - do an include to myJsp.jsp page with parameters param1=value1 and param2=value2
Example three - do an include to myJsp.jsp page with parameters param1=value1 and param2=value2
<li>include _include1.html 包含静态英文文件
		<s:include value="/_include1.html"></s:include>
		</li>
		
		<li>include _include2.html 包含静态中文文件
		<s:include value="/_include2.html"></s:include>
		</li>
		
		<li>include _include1.html 包含静态英文文件,说明%用法
		<s:set var="incPage" value="%{'/_include1.html'}" />
		<s:include value="%{#incPage}"></s:include>
		</li>

上面这个例子中使用了%,%{xxx}的意思是强制将大括号内的****认为是ongl表达式。
iterator标签

Description

Iterator will iterate over a value. An iterable value can be any of: java.util.Collection, java.util.Iterator,

Parameters

Dynamic Attributes Allowed:
false
 
Name
Required
Default
Evaluated
Type
Description
begin false 0 false Integer if specified the iteration will start on that index
end false Size of the 'values' List or array, or 0 if 'step' is negative false Integer if specified the iteration will end on that index(inclusive)
id false   false String Deprecated. Use 'var' instead
status false false false Boolean If specified, an instanceof IteratorStatus will be pushed into stack upon each iteration
step false 1 false Integer if specified the iteration index will be increased by this value on each iteration. It can be a negative value, in which case 'begin' must be greater than 'end'
value false   false String the iteratable source to iterate over, else an the object itself will be put into a newly created List
var false   false String Name used to reference the value pushed into the Value Stack

Examples

The following example retrieves the value of the getDays() method of the current object on the value stack and uses it to iterate over. The <s:property/> tag prints out the current value of the iterator.

< s:iterator value="days">
   < p >day is: < s:property /></ p >
</ s:iterator >

The following example uses a Bean tag and places it into the ActionContext. The iterator tag will retrieve that object from the ActionContext and then calls its getDays() method as above. The status attribute is also used to create an IteratorStatus object, which in this example, its odd() method is used to alternate row colours:

< s:bean name="org.apache.struts2.example.IteratorExample" var="it">
   < s:param name="day" value="'foo'"/>
   < s:param name="day" value="'bar'"/>
</ s:bean >
< p />
< table border="0" cellspacing="0" cellpadding="1">
< tr >
   < th >Days of the week</ th >
</ tr >
< p />
< s:iterator value="#it.days" status="rowstatus">
   < tr >
     < s:if test="#rowstatus.odd == true">
       < td style="background: grey">< s:property /></ td >
     </ s:if >
     < s:else >
       < td >< s:property /></ td >
     </ s:else >
   </ tr >
</ s:iterator >
</ table >

The next example will further demonstrate the use of the status attribute, using a DAO obtained from the action class through OGNL, iterating over groups and their users (in a security context). The last() method indicates if the current object is the last available in the iteration, and if not, we need to separate the users using a comma:

< s:iterator value="groupDao.groups" status="groupStatus">
     < tr class="<s:if test="#groupStatus.odd == true ">odd</ s:if >< s:else >even</ s:else >">
         < td >< s:property value="name" /></ td >
         < td >< s:property value="description" /></ td >
         < td >
             < s:iterator value="users" status="userStatus">
                 < s:property value="fullName" />< s:if test="!#userStatus.last">,</ s:if >
             </ s:iterator >
         </ td >
     </ tr >
</ s:iterator >

The next example iterates over a an action collection and passes every iterator value to another action. The trick here lies in the use of the '[0]' operator. It takes the current iterator value and passes it on to the edit action. Using the '[0]' operator has the same effect as using <s:property />. (The latter, however, does not work from inside the param tag).
< s:action name="entries" var="entries"/>
< s:iterator value="#entries.entries" >
     < s:property value="name" />
     < s:property />
     < s:push value="...">
         < s:action name="edit" var="edit" >
             < s:param name="entry" value="[0]" />
         </ s:action >
     </ push >
</ s:iterator >

A loop that iterates 5 times
< s:iterator var="counter" begin="1" end="5" >
    <!-- current iteration value (1, ... 5) -->
    < s:property value="top" />
</ s:iterator >

Another way to create a simple loop, similar to JSTL's <c:forEach begin="..." end="..." ...> is to use some OGNL magic, which provides some under-the-covers magic to make 0-n loops trivial. This example also loops five times.
< s:iterator status="stat" value="(5).{ #this }" >
    < s:property value="#stat.count" /> <!-- Note that "count" is 1-based, "index" is 0-based. -->
</ s:iterator >

A loop that iterates over a partial list
< s:iterator value="{1,2,3,4,5}" begin="2" end="4" >
    <!-- current iteration value (2,3,4) -->
    < s:property value="top" />
</ s:iterator >
值得注意的是,对于map集合进行iterator,需要按如下方式进行,value指向的map集合需要加个#号
<li>遍历集合:<br />
		<s:iterator value="{1, 2, 3}" >
			<s:property/> |
		</s:iterator>
		</li>
		<li>自定义变量:<br />
		<s:iterator value="{'aaa', 'bbb', 'ccc'}" var="x">
			<s:property value="#x.toUpperCase()"/> |
		</s:iterator>
		</li>
		<li>使用status:<br />
		<s:iterator value="{'aaa', 'bbb', 'ccc'}" status="status">
			<s:property/> | 
			遍历过的元素总数:<s:property value="#status.count"/> |
			遍历过的元素索引:<s:property value="#status.index"/> |
			当前是偶数?:<s:property value="#status.even"/> |
			当前是奇数?:<s:property value="#status.odd"/> |
			是第一个元素吗?:<s:property value="#status.first"/> |
			是最后一个元素吗?:<s:property value="#status.last"/>
			<br />
		</s:iterator>
		
		</li>
		
		<li>
		<s:iterator value="#{1:'a', 2:'b', 3:'c'}" >
			<s:property value="key"/> | <s:property value="value"/> <br />
		</s:iterator>
		</li>
		
		<li>
		<s:iterator value="#{1:'a', 2:'b', 3:'c'}" var="x">
			<s:property value="#x.key"/> | <s:property value="#x.value"/> <br />
		</s:iterator>
		</li>
		
		<li>
没有指定var,它将会将每个遍历的值存入到值栈中,遍历完后,在将其从值栈中移除,iterator中指定了status,它将会在action context中存入一个status元素。此status包含counter(iterator中总共遍历过的元素个数), index(遍历的当前元素索引,从0开始), even(遍历的当前元素是否为偶数), odd(遍历的当前元素是否为奇数), first(是否为第一个元素), last(是否为最后一个元素)等元素
append标签

Description

Component for AppendIteratorTag, which jobs is to append iterators to form an appended iterator whereby entries goes from one iterator to another after each respective iterator is exhausted of entries.

For example, if there are 3 iterator appended (each iterator has 3 entries), the following will be how the appended iterator entries will be arranged:

  1. First Entry of the First Iterator
  2. Second Entry of the First Iterator
  3. Third Entry of the First Iterator
  4. First Entry of the Second Iterator
  5. Second Entry of the Second Iterator
  6. Third Entry of the Second Iterator
  7. First Entry of the Third Iterator
  8. Second Entry of the Third Iterator
  9. Third Entry of the Third ITerator

Parameters

Dynamic Attributes Allowed:
false
 
Name
Required
Default
Evaluated
Type
Description
id false   false String Deprecated. Use 'var' instead
var false   false String The name of which if supplied will have the resultant appended iterator stored under in the stack's context

Example

public class AppendIteratorTagAction extends ActionSupport {
 
  private List myList1;
  private List myList2;
  private List myList3;
 
 
  public String execute() throws Exception {
 
      myList1 = new ArrayList();
      myList1.add(" 1 ");
      myList1.add(" 2 ");
      myList1.add(" 3 ");
 
      myList2 = new ArrayList();
      myList2.add("a");
      myList2.add("b");
      myList2.add("c");
 
      myList3 = new ArrayList();
      myList3.add("A");
      myList3.add("B");
      myList3.add("C");
 
      return "done";
  }
 
  public List getMyList1() { return myList1; }
  public List getMyList2() { return myList2; }
  public List getMyList3() { return myList3; }
< s:append var="myAppendIterator">
      < s:param value="%{myList1}" />
      < s:param value="%{myList2}" />
      < s:param value="%{myList3}" />
</ s:append >
< s:iterator value="%{#myAppendIterator}">
      < s:property />
</ s:iterator >
   generator标签

Description

NOTE: JSP-TAG

Generate an iterator based on the val attribute supplied.

NOTE: The generated iterator will ALWAYS be pushed into the top of the stack, and poped at the end of the tag.

Parameters

Dynamic Attributes Allowed:
false
 
Name
Required
Default
Evaluated
Type
Description
converter false   false org.apache.struts2.util.IteratorGenerator.Converter The converter to convert the String entry parsed from val into an object
count false   false Integer The max number entries to be in the iterator
id false   false String Deprecated. Use 'var' instead
separator true   false String The separator to be used in separating the val into entries of the iterator
val true   false String The source to be parsed into an iterator
var false   false String The name to store the resultant iterator into page context, if such name is supplied

Examples

Example One:
< pre >
Generate a simple iterator
< s:generator val="%{'aaa,bbb,ccc,ddd,eee'}">
  < s:iterator >
      < s:property />< br />
  </ s:iterator >
</ s:generator >
</ pre >
This generates an iterator and print it out using the iterator tag.
 
Example Two:
< pre >
Generate an iterator with count attribute
< s:generator val="%{'aaa,bbb,ccc,ddd,eee'}" count="3">
  < s:iterator >
      < s:property />< br />
  </ s:iterator >
</ s:generator >
</ pre >
This generates an iterator, but only 3 entries will be available in the iterator
generated, namely aaa, bbb and ccc respectively because count attribute is set to 3
 
Example Three:
< pre >
Generate an iterator with var attribute
< s:generator val="%{'aaa,bbb,ccc,ddd,eee'}" count="4" separator="," var="myAtt" />
<%
  Iterator i = (Iterator) pageContext.getAttribute("myAtt");
  while(i.hasNext()) {
      String s = (String) i.next(); %>
      <%=s%> < br />
<%    }
%>
</ pre >
This generates an iterator and put it in the PageContext under the key as specified
by the var attribute.
 
 
Example Four:
< pre >
Generate an iterator with comparator attribute
< s:generator val="%{'aaa,bbb,ccc,ddd,eee'}" converter="%{myConverter}">
  < s:iterator >
      < s:property />< br />
  </ s:iterator >
</ s:generator >
 
 
public class GeneratorTagAction extends ActionSupport {
 
   ....
 
   public Converter getMyConverter() {
      return new Converter() {
          public Object convert(String value) throws Exception {
              return "converter-"+value;
          }
      };
   }
 
   ...
 
}
</ pre >
This will generate an iterator with each entries decided by the converter supplied. With
this converter, it simply add "converter-" to each entries.
上面例四,可以指定一个convert转换器,在生成一个iterator之前,将其中每个元素的值通过转换器进行转换。
merge标签

Description

Component for MergeIteratorTag, which job is to merge iterators and successive call to the merged iterator will cause each merge iterator to have a chance to expose its element, subsequently next call will allow the next iterator to expose its element. Once the last iterator is done exposing its element, the first iterator is allowed to do so again (unless it is exhausted of entries).

Internally the task are delegated to MergeIteratorFilter

Example if there are 3 lists being merged, each list have 3 entries, the following will be the logic.

  1. Display first element of the first list
  2. Display first element of the second list
  3. Display first element of the third list
  4. Display second element of the first list
  5. Display second element of the second list
  6. Display second element of the third list
  7. Display third element of the first list
  8. Display thrid element of the second list
  9. Display third element of the thrid list

Parameters

Dynamic Attributes Allowed:
false
 
Name
Required
Default
Evaluated
Type
Description
id false   false String Deprecated. Use 'var' instead
var false   false String The name where the resultant merged iterator will be stored in the stack's context

Examples

public class MergeIteratorTagAction extends ActionSupport {
 
  private List myList1;
  private List myList2;
  private List myList3;
 
  public List getMyList1() {
      return myList1;
  }
 
  public List getMyList2() {
      return myList2;
  }
 
  public List getMyList3() {
      return myList3;
  }
 
 
  public String execute() throws Exception {
 
      myList1 = new ArrayList();
      myList1.add(" 1 ");
      myList1.add(" 2 ");
      myList1.add(" 3 ");
 
      myList2 = new ArrayList();
      myList2.add("a");
      myList2.add("b");
      myList2.add("c");
 
      myList3 = new ArrayList();
      myList3.add("A");
      myList3.add("B");
      myList3.add("C");
 
      return "done";
  }
}
< s:merge var="myMergedIterator1">
      < s:param value="%{myList1}" />
      < s:param value="%{myList2}" />
      < s:param value="%{myList3}" />
</ s:merge >
< s:iterator value="%{#myMergedIterator1}">
      < s:property />
</ s:iterator >
subset标签

Description

NOTE: JSP-TAG

A tag that takes an iterator and outputs a subset of it. It delegates to org.apache.struts2.util.SubsetIteratorFilter internally to perform the subset functionality.

Parameters

Dynamic Attributes Allowed:
false
 
Name
Required
Default
Evaluated
Type
Description
count false   false Integer Indicate the number of entries to be in the resulting subset iterator
decider false   false org.apache.struts2.util.SubsetIteratorFilter.Decider Extension to plug-in a decider to determine if that particular entry is to be included in the resulting subset iterator
id false   false String Deprecated. Use 'var' instead
source false   false String Indicate the source of which the resulting subset iterator is to be derived base on
start false   false Integer Indicate the starting index (eg. first entry is 0) of entries in the source to be available as the first entry in the resulting subset iterator
var false   false String The name to store the resultant iterator into page context, if such name is supplied

Examples

public class MySubsetTagAction extends ActionSupport {
      public String execute() throws Exception {
         l = new ArrayList();
         l.add( new Integer( 1 ));
         l.add( new Integer( 2 ));
         l.add( new Integer( 3 ));
         l.add( new Integer( 4 ));
         l.add( new Integer( 5 ));
         return "done";
      }
 
 
      public Integer[] getMyArray() {
         return a;
      }
 
      public List getMyList() {
         return l;
       }
 
      public Decider getMyDecider() {
      return new Decider() {
          public boolean decide(Object element) throws Exception {
              int i = ((Integer)element).intValue();
              return (((i % 2 ) == 0 )? true : false );
          }
      };
      }
  }
<!-- s: List basic -->
    < s:subset source="myList">
       < s:iterator >
          < s:property />
       </ s:iterator >
    </ s:subset >
<!-- B: List with count -->
    < s:subset source="myList" count="3">
       < s:iterator >
           < s:property />
       </ s:iterator >
     </ s:subset >
<!--  C: List with start -->
      < s:subset source="myList" count="13" start="3">
         < s:iterator >
           < s:property />
         </ s:iterator >
      </ s:subset >
<!--  D: List with var -->
      < s:subset var="mySubset" source="myList" count="13" start="3" />
      <%
          Iterator i = (Iterator) pageContext.getAttribute("mySubset");
          while(i.hasNext()) {
      %>
      <%=i.next() %>
      <%  } %>
<!--  D: List with Decider -->
     < s:subset source="myList" decider="myDecider">
            < s:iterator >
                 < s:property />
            </ s:iterator >
     </ s:subset >






Name
Required
Default
Evaluated
Type
Description
default false   false String The default value to be used if value attribute is null
escape false true false Boolean Deprecated. Use 'escapeHtml'. Whether to escape HTML
escapeCsv false false false Boolean Whether to escape CSV (useful to escape a value for a column)
escapeHtml false true false Boolean Whether to escape HTML
escapeJavaScript false false false Boolean Whether to escape Javascript
escapeXml false false false Boolean Whether to escape XML
value false <top of stack> false Object Value to be displayed

Examples

< s:push value="myBean">
     <!-- Example 1: -->
     < s:property value="myBeanProperty" />
 
     <!-- Example 2: --> TextUtils
     < s:property value="myBeanProperty" default="a default value" />
</ s:push >
Example 1 prints the result of myBean's getMyBeanProperty() method.
Example 2 prints the result of myBean's getMyBeanProperty() method and if it is null, print 'a default value' instead.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值