ognl <s:subset>标签用法

subset标签用于取得集合的子集,该标签的底层通过org.apache.Struts2.util.Subset.IteratorFilter类提供实现。使用subset标签可以指定如下几个属性:

 

count:可选属性,指定子集中元素的个数,默认取得源集合的所有元素

 

source:可选属性,指定源集合,如果不指定,默认取得valueStack栈顶的集合,一般都会指定

 

start:可选属性,指定从源集合的第几个元素开始截取,,默认从第一个元素(start=0)开始

 

decider:可选属性,由开发者自己决定是否选中该元素

 

一般用法如下:

 

<%@ page contentType="text/html; charset=GBK" language="java"%>

<%@taglib prefix="s" uri="/struts-tags"%>

<html>

<head>

<title>使用s:subset标签截取集合元素</title>

</head>

<body>

<table border="1" width="200">

<s:subset source="{'Java','Spring2.0','J2EE','Ajax','WebWork'}"

start="1" count="3">

<s:iterator status="st">

<tr <s:if test="#st.odd">style="background-color:#bbbbbb"</s:if>>

   <td><s:property/></td>

</tr>

</s:iterator>

</s:subset>

</table>

</body>

</html>

上面的代码的source属性指定的集合包含了5个元素,通过subset从第2个元素开始截取,只取出其中3个元素。

 

此外,Struts2还允许开发者决定截取标准,开发者只需要实现一个Decider类,实现SubsetIteratorFilter.Decider接口中的boolean decide(Object element)方法,如果该方法返回真,则表明该元素将被选入子集中。看如下代码:

 

package lee;

 

import org.apache.struts2.util.SubsetIteratorFilter;

public class MyDecider implements SubsetIteratorFilter.Decider

{

public boolean decide(Object element) throws Exception

{

   String str = (String)element;

   return str.indexOf("J2EE") > 0;

}

}

这里要求过滤不包含“J2EE”的元素,JSP页面代码如下:

 

<%@ page contentType="text/html; charset=GBK" language="java"%>

<%@taglib prefix="s" uri="/struts-tags"%>

<html>

<head>

<title>使用s:subset标签截取集合元素</title>

</head>

<body>

<s:bean id="mydecider" name="lee.MyDecider"/>

<table border="1" width="200">

<s:subset

source="{'Java','Spring2.0','J2EE','Ajax','WebWork'}"

decider="#mydecider">

<s:iterator status="st">

<tr <s:if test="#st.odd">style="background-color:#bbbbbb"</s:if>>

   <td><s:property/></td>

</tr>

</s:iterator>

</s:subset>

</table>

</body>

</html>

猜你喜欢

转载自wdyr123.iteye.com/blog/1673914
今日推荐