Struts2 控制标签(九)取集合中的部分元素为子集合

1.s:subset标签概述
属性名 类型 说明
count Integer 获取原始集合中元素的个数
decider org.apache.struts2.util.SubsetIteratorFilter.Decider 过滤条件
source String 源集合数据
Start Integer 从哪个位置开始,以索引0开始
var String 新的集合存储在page上下文中的名称

2.创建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;
    }
}

3.创建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 ShowSubSetList 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 "showsubsetlist";
    }

}

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:subset source="listBBS" var="newSubList" count="2" start="1">
         <s:iterator value="#attr.newSubList" var="BBSBean">
            <s:property value="#BBSBean.author"/>
         </s:iterator>
     </s:subset>
  </body>
</html>

注释: <s:subset source="listBBS" var="newSubList" count="2" start="1">
source:代表数据的来源;Start:代表从第几个元素开始截取;
count:代表截取几个元素;var:代表截取出来的集合名称。
由于 subset的var是放在page页面级的变量,所以要使用attr将page级中的newSubList取出在进行遍历:即<s:iterator value="#attr.newSubList" var="BBSBean">。
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="showsubsetlist" class="com.wl.control.tagsAction.ShowSubSetList">
           <result name="showsubsetlist">/controlTags/showsubsetlist.jsp</result>
       </action>
   </package>
</struts>

6.运行结果:

Struts2 控制标签(九)取集合中的部分元素为子集合

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