Spring MVC将ArrayList传递回控制器 [英] Spring MVC passing ArrayList back to controller

查看:117
本文介绍了Spring MVC将ArrayList传递回控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to Spring. I display a list with users. Every row has a checkbox for removing the users.

控制器:

@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用户为空。
我也尝试将列表包装在一个新类中,但我得到相同的结果。

The list is rendered correctly. If i press the "Delete user(s)" button. The @modelAttribute users is empty. I also tried wrapping the list in a new class, but i get the same results.

任何想法?

推荐答案

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

Your ModelAttribute is empty as there is no form data binding happening from your jsp to your model attribute. Take a look at how Spring sample for binding collections "http://developer.ucsd.edu/develop/user-interface/building-a-form/form-binding-with-collections.html". This will help you to understand.

大多数Spring应用程序通常使用 form:input 路径参数来做数据绑定。

Most of the Spring application typically uses form:input with "path" parameter to do data binding.

这篇关于Spring MVC将ArrayList传递回控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆