【发布时间】:2016-02-05 21:20:30
【问题描述】:
在当前控制器中,我用无法访问使用应用程序的用户集合填充我的 userWrapper,并将其作为属性发送到将呈现的 jsp。
@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 集合用户的用户对象绑定的复选框。我的 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 market@test.ra pass 2016-02-02 21:46:40.0
2 tara@ra.ra passwd 2016-02-03 20:37:18.0
每行附近都有复选框,当我检查一些行并将帖子发送到控制器时,我只捕获这些值吗?复选框有效。
[User [id=0, email=null, password=null, enabled=true],
User [id=0, email=null, password=null, enabled=false]]
为什么值 null 或 0? 这是我的 UserWrapper 类和用户构造函数:
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;
...
}
更新 我还要问:如何将对象从控制器发送到 jsp,在那里更改该对象,然后将这个已更改的对象发送回控制器...
更新
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[] { "/" };
}
}
【问题讨论】:
标签: java spring jsp spring-mvc