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>

  

转载于:https://www.cnblogs.com/wenqiangwu/archive/2013/01/16/struts2_jsp_list.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值