zoukankan      html  css  js  c++  java
  • Struts2和JSP之间,List对象的传递

    1. 需要传递的POJO对象.

    /**
     * @author cheney
     * 
     * @date Jan 7, 2013
     */
    public class User {
    
        private String id;
        private String name;
        private String password;
    
        /**
         * @return the id
         */
        public String getId() {
            return id;
        }
    
        /**
         * @param id
         *            the id to set
         */
        public void setId(String id) {
            this.id = id;
        }
    
        /**
         * @return the name
         */
        public String getName() {
            return name;
        }
    
        /**
         * @param name
         *            the name to set
         */
        public void setName(String name) {
            this.name = name;
        }
    
        /**
         * @return the password
         */
        public String getPassword() {
            return password;
        }
    
        /**
         * @param password
         *            the password to set
         */
        public void setPassword(String password) {
            this.password = password;
        }
    
    }

    2. Action中的初始化和对jsp页面提交对象的接受

    import java.util.LinkedList;
    import java.util.List;
    
    import org.apache.struts2.convention.annotation.Action;
    import org.apache.struts2.convention.annotation.Result;
    
    import com.opensymphony.xwork2.conversion.annotations.ConversionRule;
    import com.opensymphony.xwork2.conversion.annotations.TypeConversion;
    import com.tydic.base.BaseAction;
    
    /**
     * @author cheney
     * 
     * @date Aug 15, 2012
     */
    public class UserAction extends BaseAction {
        private List<User> mapList;
        
    
        /**
         * 
         */
        private static final long serialVersionUID = 3551905855359325103L;
    
        @Action(value = "/service/flowcard/user/init", results = { @Result(name = SUCCESS, location = "/jsp/flowcard/u_index.jsp") })
        public String init() throws Exception {
            System.out.println("init.................");
            mapList = new LinkedList<User>();
    
            User user1 = new User();
            user1.setId("1");
            user1.setName("chen");
            user1.setPassword("aa");
    
            User user2 = new User();
            user2.setId("2");
            user2.setName("fa");
            user2.setPassword("11");
    
            User user3 = new User();
            user3.setId("3");
            user3.setName("gaaa");
            user3.setPassword("222");
    
            User user4 = new User();
            user4.setId("4");
            user4.setName("mms");
            user4.setPassword("w2w");
    
            mapList.add(user1);
            mapList.add(user2);
            mapList.add(user3);
            mapList.add(user4);
    
            return SUCCESS;
        }
    
        @Action(value = "/service/flowcard/user/register", results = { @Result(name = SUCCESS, location = "/jsp/flowcard/u_succ.jsp") })
        public String register() throws Exception {
            System.out.println("register.................");
    
            // System.out.println(mapList.size());
    
            for (int i = 0; i < mapList.size(); i++) {
                System.out.println(mapList.get(i).getId());
                System.out.println(mapList.get(i).getName());
                System.out.println(mapList.get(i).getPassword());
    
                System.out.println("=============================");
            }
    
            return SUCCESS;
        }
    
        /**
         * @return the mapList
         */
        public List<User> getMapList() {
            return mapList;
        }
    
        /**
         * @param mapList
         *            the mapList to set
         */
        @TypeConversion(rule = ConversionRule.COLLECTION, converter = "com.tydic.flowcard.action.User")
        public void setMapList(List<User> mapList) {
            this.mapList = mapList;
        }
    
    }

    3. JSP页面的封装

    <body>
    		<div align="center">
    		
    			<form id="formId" action="/service/flowcard/user/register.action" method="post">
    			
    				<c:forEach var="ml" items="${mapList}" varStatus="status">
    					<input type="hidden" value="${ml.id}" name="mapList[${status.index}].id" />
    					<input type="hidden" value="${ml.name}" name="mapList[${status.index}].name" />
    					<input type="hidden" value="${ml.password}" name="mapList[${status.index}].password" />
    				</c:forEach>
    				
    				<input type="submit" value="SUBMIT" />
    			</form>
    			
    		</div>
    	</body>
    

      

  • 相关阅读:
    常用sql(转)
    dos
    VS调试 DataTable (转载)
    JS 对象 数组求并集,交集和差集
    js跳出循环的方法区别(break,continue,return)(转载)
    echarts 曲线平滑配置
    js 动态 批量 创建局部或全局变量 (转载)
    电脑 闪屏 无法启动的原因
    js switch case 判断的是绝对相对===,值和类型都要相等
    无法创建“匿名类型”类型的常量值。此上下文仅支持基元类型或枚举类型。
  • 原文地址:https://www.cnblogs.com/wenqiangwu/p/struts2_jsp_list.html
Copyright © 2011-2022 走看看