码迷,mamicode.com
首页 > 编程语言 > 详细

struts2绑定对象数组

时间:2015-05-01 22:37:40      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

1、首先写一个要绑定的对象

User.java

package com.zjd.getExcel.entity;

/**
 * Created by dong on 15-4-19.
 */
public class User {

    public  User(){}

    private Integer id;
    private String username;
    private String sex;
    private String email;
    private String password;
    private int count;


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

2、前端页面

<html>
<head>
    <title>test</title>
</head>

<body>
     <form action="test" method="post">

                     <%
                         for (int i =0 ;i < 2 ; i++){
                             %>
                                     <input type="text" name="users[<%=i%>].username" />
                                     <input type="text" name="users[<%=i%>].sex" />
                                     <input type="text" name="users[<%=i%>].email" />
                                     <input type="text" name="users[<%=i%>].password" />
                             <%
                         }
                     %>

                <input type="submit" value="提交" />
     </form>
</body>
</html>
3、接受前端传过来的对象数组users

test.java(取对象数组时,用List<User>得到)

@Component
public class Test extends ActionSupport {

    private List<User> users;
    @Override
    public String execute() throws Exception {
        System.out.println(users.toString()+"-----");
        for (User user:users){
            System.out.println(user.getUsername()+"---"+user.getEmail()+"--"+user.getPassword()+"-"+user.getSex());
        }
        return Action.SUCCESS;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }
}

参考链接

struts2绑定对象数组

标签:

原文地址:http://blog.csdn.net/u010286751/article/details/45422705

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!