0
在當前控制器中,我將userWrapper填充爲沒有訪問使用應用程序的用戶的集合,並將其作爲屬性發送給將呈現的jsp。Jsp表單傳遞一個空對象給Spring控制器
@RequestMapping(value = "/cockpit")
public String cockpit(Model model, UserWrapper receivedUserWrapper) {
siftOutEnabled(receivedUserWrapper);
try {
List<User> users = userService.findAllUsersWhoNotAccepted();
UserWrapper userWrapper = new UserWrapper();
userWrapper.setUsers(users);
model.addAttribute("wrapper", userWrapper);
model.addAttribute("users", userWrapper.getUsers());
} catch (UsersNotFoundException e) {
}
return "cockpit";
}
在jsp中存在與UserWrapper集合用戶的User對象綁定的複選框。我的jsp頁面的樣子:
<form:form method="POST" commandName="wrapper" action="cockpit">
<table>
<tr>
<th>id</th>
<th>mail</th>
<th>pass</th>
<th>registrationDate</th>
<th></th>
</tr>
<c:forEach items="${users}" var="user" varStatus="loop">
<tr>
<td>${user.id}</td>
<td>${user.email}</td>
<td>${user.password}</td>
<td>${user.registrationDate}</td>
<td><form:checkbox path="users[${loop.index}].enabled" />
</tr>
</c:forEach>
</table>
<input type="submit" name="submit" value="submit" />
</form:form>
這個jsp頁面渲染正確的結果:
id mail pass registrationDate
1 [email protected] pass 2016-02-02 21:46:40.0
2 [email protected] passwd 2016-02-03 20:37:18.0
靠近每行存在checbox,當我檢查了一些線,並送崗位到控制器,在那裏,我只有這些趕值?複選框工作。
[User [id=0, email=null, password=null, enabled=true],
User [id=0, email=null, password=null, enabled=false]]
爲什麼值爲null或0? 這裏是我UserWrapper類,和用戶consrtuctor:
public class User implements Serializable {
...
public User(long id, String email, String password, Date registrationDate, boolean enabled) {
this.setEmail(email);
this.setPassword(password);
this.registrationDate = registrationDate;
this.setEnabled(enabled);
roles = new HashSet<>();
}
...
}
public class UserWrapper {
private boolean checked;
private List<User> users;
...
}
UPDATE 我還要問:如何從控制器發送對象JSP中對象存在,變化,那麼這已經改變了對象發送回控制器。 ..
UPDATE
public class SpringMvcIntializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { ApplicationConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebMvcConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
感謝。它的工作 – viavad
也質疑如何將該日期傳遞給控制器? – viavad