Struts2 控制标签(十)按自定义条件取集合中的匹配元素为子集合

1.创建JavaBean实体类
package com.wl.control.Bean;

public class BBS {

private String title;

private String author;

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}

2.创建Action实体类
package com.wl.control.tagsAction;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.wl.control.Bean.BBS;

public class ShowWhereSubSetList extends ActionSupport {

private List listBBS=new ArrayList();

public List getListBBS() {
return listBBS;
}

public void setListBBS(List listBBS) {
this.listBBS = listBBS;
}

@Override
public String execute() throws Exception {

BBS bbs1=new BBS();
bbs1.setAuthor("张三");
bbs1.setTitle("我是张三");

BBS bbs2=new BBS();
bbs2.setAuthor("李四");
bbs2.setTitle("我是李四");

BBS bbs3=new BBS();
bbs3.setAuthor("张九");
bbs3.setTitle("我是张九");

listBBS.add(bbs1);
listBBS.add(bbs2);
listBBS.add(bbs3);
return "showwheresubsetlist";
}

}

3.创建自定义条件过滤的Java类
package com.wl.operator.util;

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

import com.wl.control.Bean.BBS;

public class WhereName implements Decider {

public boolean decide(Object arg0) throws Exception {

BBS bbs=(BBS)arg0;
return bbs.getAuthor().contains("张");
}

}

注释:[color=red][b]这个条件过滤类要实现Decider接口,并且重写decide()方法。[/b][/color]
4.创建JSP文件
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<body>
<s:bean name="com.wl.operator.util.WhereName" var="whereref"></s:bean>
<s:subset source="listBBS" var="newSubList" decider="whereref">
<s:iterator value="#attr.newSubList" var="BBSBean">
<s:property value="#BBSBean.author"/>
</s:iterator>
</s:subset>

</body>
</html>

5.修改struts.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="Struts2.1" extends="struts-default">
<action name="showwheresubsetlist" class="com.wl.control.tagsAction.ShowWhereSubSetList">
<result name="showwheresubsetlist">/controlTags/showwheresubsetlist.jsp</result>
</action>
</package>
</struts>

6.运行结果:

[img]http://dl.iteye.com/upload/attachment/380472/715f4745-03e3-306d-aaac-3e76bfe53bef.png[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值