在业务控制方法中收集List<JavaBean>参数

@Controller
@RequestMapping(value="/user")
public class UserAction {
    @RequestMapping(value="/addAll")
    public String addAll(Bean bean,Model model) throws Exception{
        for(User user : bean.getUserList()){
            System.out.println(user.getName()+":"+user.getGender());
        }
        model.addAttribute("message","批量增加用户成功");
        return "/success.jsp";
    }
}
public class Bean {
    private List<User> userList = new ArrayList<User>();
    public Bean(){}
    public List<User> getUserList() {
        return userList;
    }
    public void setUserList(List<User> userList) {
        this.userList = userList;
    }
}

    registerAll.java

<form action="${pageContext.request.contextPath}/user/addAll.action" method="POST"> 
         
        姓名:<input type="text" name="userList[0].name" value="哈哈"/>
        性别:<input type="text" name="userList[0].gender" value="男"/>
        <hr/>
        
        姓名:<input type="text" name="userList[1].name" value="呵呵"/>
        性别:<input type="text" name="userList[1].gender" value="男"/>
        <hr/>

        姓名:<input type="text" name="userList[2].name" value="嘻嘻"/>
        性别:<input type="text" name="userList[2].gender" value="女"/>
        <hr/>
        
        <input type="submit" value="批量注册"/>
        
    </form>

 

posted on 2018-12-04 12:03  LoaderMan  阅读(190)  评论(0编辑  收藏  举报

导航