index.jsp里:<a href="listAction1!test1">集合标签</a><br>
struts.xml里:
<action name="listAction1" class="com.xsl.action.ListAction1">
<result name="test1">/list.jsp</result>
</action>
ListAction1.java里:
1 package com.xsl.action; 2 import java.util.ArrayList; 3 import java.util.List; 4 import javax.servlet.http.HttpServletRequest; 5 import org.apache.struts2.interceptor.ServletRequestAware; 6 import com.opensymphony.xwork2.ActionSupport; 7 import com.xsl.entity.Dog; 8 import com.xsl.entity.Users; 9 10 public class ListAction1 extends ActionSupport implements ServletRequestAware { 11 private Users users; 12 private HttpServletRequest request; 13 private List<Users> ulist; 14 public String test1() { 15 users = new Users(2,"jack"); 16 17 List<Users> ulist = new ArrayList<Users>(); 18 ulist.add(new Users(1,"rose1")); 19 ulist.add(new Users(2,"rose2")); 20 ulist.add(new Users(3,"rose3")); 21 ulist.add(new Users(4,"rose4")); 22 ulist.add(new Users(5,"rose5")); 23 ulist.add(new Users(6,"rose6")); 24 request.setAttribute("ulist", ulist); 25 26 List<Dog> list = new ArrayList<Dog>(); 27 list.add(new Dog(1,"dog1")); 28 list.add(new Dog(2,"dog2")); 29 list.add(new Dog(3,"dog3")); 30 list.add(new Dog(4,"dog4")); 31 list.add(new Dog(5,"dog5")); 32 list.add(new Dog(6,"dog6")); 33 //绑定用户(一个用户有多条狗) 34 users.setDids(new int[]{1,3,6}); 35 request.setAttribute("dlist", list); 36 return "test1"; 37 } 38 public void setServletRequest(HttpServletRequest request) { 39 this.request = request; 40 } 41 public Users getUsers() { 42 return users; 43 } 44 public void setUsers(Users users) { 45 this.users = users; 46 } 47 public HttpServletRequest getRequest() { 48 return request; 49 } 50 public void setRequest(HttpServletRequest request) { 51 this.request = request; 52 } 53 public List<Users> getUlist() { 54 return ulist; 55 } 56 public void setUlist(List<Users> ulist) { 57 this.ulist = ulist; 58 } 59 }
list.jsp里:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib uri="/struts-tags" prefix="s" %> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <title>list.jsp</title> 11 </head> 12 13 <body> 14 <!-- 15 label="用户名"是用于设置标签的提示语 16 list="#request.ulist"用于设置需要进行迭代的集合对象 17 listKey="id"用于设置select这个标签下option的value值 18 listValue="uname"用于设置select这个标签option的显示在浏览器上的值 19 name="users.id"作用:用于判断users.uid和进行迭代那一个uid相等,则让这个users对象处于被选中的状态 20 用于提交表单时,action进行收集表单对应的数据 21 --> 22 <s:select lable="用户名" list="#request.ulist" listKey="id" listValue="uname" name="users.id"> 23 </s:select> 24 <br><br> 25 <h5>填写表单,提交表单</h5> 26 <form action="listAction1!test1" method="post"> 27 <s:checkboxlist list="#request.dlist" listKey="did" listValue="dname" name="users.dids" > 28 </s:checkboxlist><br> 29 <s:submit value="提交"></s:submit> 30 </form> 31 <s:debug></s:debug> 32 </body> 33 </html>
最终显示: