Spring MVC将ArrayList传递回控制器

时间:2015-02-19 14:52:57

标签: java spring jsp spring-mvc

我是Spring的新手。我显示一个用户列表。每行都有一个用于删除用户的复选框。

控制器:

@Controller
public class AdminController {

    @Autowired
    private UserDao userDao;

    @RequestMapping(value = "/admin", method = RequestMethod.GET)
    public ModelAndView adminPage() {
        ModelAndView model = new ModelAndView();
        model.addObject("users", userDao.findAll());
        model.setViewName("admin");
        return model;

    }

    @RequestMapping(value = "admin/remove", method = RequestMethod.POST)
    public ModelAndView removeUser(@ModelAttribute(value = "users") ArrayList<User> users) {
        ModelAndView model = new ModelAndView();
        //UPDATE USERS HERE 
        model.setViewName("redirect:/admin");
        return model;

    }

JSP:

<form:form action="/admin/remove" method="POST"  modelAttribute="users">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Firstname</th>
                        <th>Lastname</th>
                        <th>Email/login</th>
                        <th>Profession</th>
                        <th>Select<th>
                    </tr>
                </thead>
                <tbody>
                    <c:forEach var="user" items="${users}">
                        <tr>
                            <td>${user.firstName}</td>
                            <td>${user.lastName}</td>
                            <td>${user.login}</td>
                            <td>${user.profession}</td>
                            <td><input type="checkbox" value="${user.delete}"/></td>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>            
            <input type="submit" value="Delete user(s)" class="btn-danger" />
            <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
            </form:form>

列表正确呈现。如果我按“删除用户”按钮。 @modelAttribute用户为空。 我也尝试将列表包装在一个新类中,但我得到了相同的结果。

有什么想法吗?

4 个答案:

答案 0 :(得分:7)

感谢minion,我找到了答案

打包机:

public class UserListWrapper {

private ArrayList<User> users;

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

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

控制器:

@Controller
public class AdminController {

@Autowired
private UserDao userDao;

@RequestMapping(value = "/admin", method = RequestMethod.GET)
public ModelAndView adminPage() {
    ModelAndView model = new ModelAndView();
    UserListWrapper wrapper = new UserListWrapper();
    wrapper.setUsers(new ArrayList<User>(userDao.findAll()));
    model.addObject("userListWrapper",wrapper);

    model.setViewName("admin");
    return model;

}

@RequestMapping(value = "admin/remove", method = RequestMethod.POST)
public ModelAndView removeUser(@ModelAttribute(value = "userListWrapper") UserListWrapper userListWrapper) {
    ModelAndView model = new ModelAndView();
    userDao.removeFlaggedUsers(userListWrapper.getUsers());
    model.setViewName("redirect:/admin");
    return model;

}

}

查看:

<form:form action="/admin/remove" method="POST"  modelAttribute="userListWrapper">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>First name</th>
                    <th>Last name</th>
                    <th>Email/login</th>
                    <th>Profession</th>
                    <th>Select<th>
                </tr>
            </thead>
            <tbody>
                <c:forEach varStatus="us" var="user" items="${userListWrapper.users}" >
                    <tr>
                        <td><form:input type="hidden" path="users[${us.index}].firstName"/>${user.firstName}</td>
                        <td><form:input type="hidden" path="users[${us.index}].lastName"/> ${user.lastName}</td>
                        <td><form:input type="hidden" path="users[${us.index}].login"/>${user.login}</td>
                        <td><form:input type="hidden" path="users[${us.index}].profession"/>${user.profession}</td>
                        <td><form:checkbox path="users[${us.index}].delete" value="${user.delete}"/></td>
         <form:input type="hidden" path="users[${us.index}].id"/>
                    </tr>
                </c:forEach>
            </tbody>
        </table>            
        <input type="submit" value="Delete user(s)" class="btn-danger" />
        <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
        </form:form>

谢谢!

编辑:别忘了添加你没有显示的字段。

例如:

如果您不添加id,则删除将无效,因为返回的User对象中的id将为NULL。

答案 1 :(得分:5)

您的ModelAttribute为空,因为从您的jsp到您的模型属性没有形式数据绑定。看一下Spring样本如何绑定集合“http://developer.ucsd.edu/develop/user-interface/building-a-form/form-binding-with-collections.html”。这有助于您理解。

大多数Spring应用程序通常使用 form:input 和“ path ”参数来进行数据绑定。

答案 2 :(得分:1)

您应该围绕spring-mvc select 标记构建您的功能。然而,几乎没有变化,将列表推送到POJO类,例如

public class FormBean {

    private List<String> users;

    public FormBean() {

    }

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

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

将您的映射更改为

@RequestMapping(value = "admin/remove", method = RequestMethod.POST)
    public ModelAndView removeUser(@ModelAttribute(value = "formBean") FormBean formBean) {

最后,使用spring select 标记交换你的 c:forEach ,这样就像

<form:form action="/admin/remove" method="POST"  modelAttribute="formBean">
  ...
 <form:select path="users" items="${users}" multiple="true" />
  ...
</form>

答案 3 :(得分:0)