簡體   English   中英

Spring MVC將ArrayList傳遞回控制器

[英]Spring MVC passing ArrayList back to controller

我是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用戶為空。 我也嘗試將列表包裝在一個新類中,但我得到了相同的結果。

有任何想法嗎?

感謝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。

您的ModelAttribute為空,因為從您的jsp到您的模型屬性沒有發生表單數據綁定。 看一下Spring樣本如何綁定集合“ http://developer.ucsd.edu/develop/user-interface/building-a-form/form-b​​inding-with-collections.html ”。 這有助於您理解。

大多數Spring應用程序通常使用form:input with“ path ”參數來進行數據綁定。

您應該圍繞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 選擇標簽交換你的c:forEach ,就像這樣

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM