jeudi 19 février 2015

Spring MVC passing ArrayList back to controller

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


Controller:



@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();
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>


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.


Any ideas?


Aucun commentaire:

Enregistrer un commentaire