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>

  

posted @ 2013-01-16 09:39  牛顿的苹果  阅读(21554)  评论(1编辑  收藏  举报