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("张");
    }

}

注释: 这个条件过滤类要实现Decider接口,并且重写decide()方法。
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.运行结果:

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

你可能感兴趣的:(java,apache,bean,struts,bbs)