struts2的集合标签

原文链接: http://www.cnblogs.com/xsl1995/p/6649696.html

index.jsp里:集合标签

struts.xml里:


  /list.jsp

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 ulist;
14     public String test1() {
15         users = new Users(2,"jack");
16         
17         List ulist = new ArrayList();
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 list = new ArrayList();
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 getUlist() {
54         return ulist;
55     }
56     public void setUlist(List 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.jsptitle>
11   head>
12   
13   <body>
14       
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>

最终显示:

struts2的集合标签_第1张图片

 

转载于:https://www.cnblogs.com/xsl1995/p/6649696.html

你可能感兴趣的:(struts2的集合标签)