springmvc接收List和数组

来源:互联网 发布:淘宝评价不能发图片 编辑:程序博客网 时间:2024/06/07 19:16
  • 用例
    • 批量提交数据,更新修改后的多条数据
  • 创建实体类
  • UserEntity.java
public class UserEntity implements java.io.Serializable {    private static final long serialVersionUID = 1168696728446841996L;    private String name;    private Integer age;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public static long getSerialversionuid() {        return serialVersionUID;    }}
  • UserEntityVO .java定义了一个包含List的实体类,springmvc不能直接用List作为形参接收List
public class UserEntityVO {    private List<UserEntity> userList;    public List<UserEntity> getUserList() {        return userList;    }    public void setUserList(List<UserEntity> userList) {        this.userList = userList;    }}
  • 控制层定义
public String update(UserEntityVO entity) {        List<UserEntity> list = entity.getUserList();        return null;}
  • 页面定义:注意表单提交时input框的name属性的命名一定要注意格式
<form action="user/update" method="post">    <table>        <thead>            <tr>                <th>姓名</th>                <th>年龄</th>                <th><button type="submit" >更新</button></th>            </tr>        </thead>        <tbody>            <tr>                <td><input type="text" name="userList[0].name"/></td>                <td><input type="text" name="userList[0].age"/></td>            </tr>            <tr>                <td><input type="text" name="userList[1].name"/></td>                <td><input type="text" name="userList[1].age"/></td>            </tr>        </tbody>    </table></form>
  • springmvc控制层接收数组
    • 方法的形参定义一个数组:例如array
    • 页面input传递时name属性的命名和形参中的命名保持一致
原创粉丝点击