Struts 2中标签的用法

时间:2021-06-19 17:56:56

例1

下面的代码subset.jsp截取了原集合从第二个元素(start=1)开始的三个元素(count=3)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>subset.jsp</title>
</head>

<body>
<s:subset source="{'C++程序设计','VC++实用教程','SQL Server应用','C#程序设计','Java程序设计'}" start="1" count="3">
<s:iterator>
<s:property/><br>
</s:iterator>
</s:subset>
</body>
</html>

显示效果如下:
Struts 2中标签的用法

例2

使用decider自定义截取方式

1.实现Decider接口

package org.common;

import org.apache.struts2.util.SubsetIteratorFilter.Decider;

public class SubsetTagDecider implements Decider{

public boolean decide(Object arg0) throws Exception {
//包含C的图书名称
return arg0.toString().contains("C");
}
}

2.应用subset标签的decider属性

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>subset2.jsp</title>
</head>

<body>
<s:bean name="org.common.SubsetTagDecider" id="subsetTagDecider"/>
<s:subset source="{'C++程序设计','VC++实用教程','SQL Server应用','C#程序设计','Java程序设计'}" decider="subsetTagDecider">
<s:iterator>
<s:property/><br>
</s:iterator>
</s:subset>
</body>
</html>

显示效果如下:
Struts 2中标签的用法