Pau*_*uhn 13 spring spring-mvc
我知道围绕这个主题已有问题,但我还没弄清楚如何解决以下问题:
我有一个用户/角色关系,我想列出JSP中的所有可用角色作为复选框项,其中选中了用户分配的复选框.但是,不检查匹配的项目(这里我使用的是Spring 3.1).
从User对象中提取:
private Set<RoleEntity> roles = new HashSet<RoleEntity>();
Run Code Online (Sandbox Code Playgroud)
从Spring Controller中提取(将用户对象和角色列表添加到Model):
UserEntity userEntity = userEntityService.findById(UserEntity.class, new Long(id));
model.addAttribute("userAttribute", userEntity);
List<RoleEntity> roleList = roleEntityService.findAll();
model.addAttribute("roleList", roleList);
Run Code Online (Sandbox Code Playgroud)
从JSP中提取:
<form:form modelAttribute="userAttribute" method="POST" action="${saveUrl}">
...
<table align="center">
<tr>
<td>ID</td>
<td>Role Name</td>
</tr>
<c:forEach items="${roleList}" var="role" varStatus="status">
<tr>
<td><form:checkbox path="roles" value="${role}" label="${role.id}" /></td>
<td><c:out value="${role.name}" /></td>
</tr>
</c:forEach>
</table>
...
</form:form>
Run Code Online (Sandbox Code Playgroud)
Spring MVC文档说明了这一点:当绑定值是array或java.util.Collection类型时,如果绑定的Collection中存在已配置的setValue(Object)值,则输入(复选框)将标记为"checked".
这不是这个案子吗?我在这里错过了什么?
非常感谢.
保罗
Bog*_*dan 20
我的猜测是你错过了RoleEntity类equals和hashcode方法的实现.
当绑定值的类型为array或java.util.Collection时,如果绑定的Collection中存在已配置的setValue(Object)值,则输入(复选框)将标记为"已检查".
这是正确的,但要检查HashSet您需要的存在equals并hashcode正确实施.
就像快速测试一样,看看是否存在问题,请替换此行:
model.addAttribute("roleList", roleList);
Run Code Online (Sandbox Code Playgroud)
用这一行:
model.addAttribute("roleList", userEntity.getRoles());
Run Code Online (Sandbox Code Playgroud)
你检查了所有的复选框吗?如果是,那么您没有提供自己的equals,hashcode并且使用默认的(继承自的Object).
默认值equals比较identity,这意味着变量与另一个变量保持相同的实例.平等意味着两个不同的对象包含相同的状态或具有相同的含义,可以这么说.
使用model.addAttribute("roleList", userEntity.getRoles())触发器的默认equals方法返回true,因为您检查列表中存在的列表和值是否相同(两个相同的对象始终相等).
但在你的情况下,你使用userEntityService.findById一个而roleEntityService.findAll另一个意味着不同的对象.此时,您必须使用适当的相等测试而不是身份.
你有equals/ hashcode实施?
根据您的代码,这是一个有效的示例:
控制器:
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class SomeController {
@RequestMapping(value = "/something", method = { RequestMethod.GET, RequestMethod.POST })
public String handle(Model model) {
UserEntity userEntity = new UserEntity();
userEntity.setRoles(new HashSet<RoleEntity>());
Collections.addAll(userEntity.getRoles(),
new RoleEntity(1, "one"),
new RoleEntity(3, "three"));
model.addAttribute("userAttribute", userEntity);
List<RoleEntity> roleList = Arrays.asList(
new RoleEntity(1, "one"),
new RoleEntity(2, "two"),
new RoleEntity(3, "three")
);
model.addAttribute("roleList", roleList);
return "view";
}
}
Run Code Online (Sandbox Code Playgroud)
用户类:
import java.util.HashSet;
import java.util.Set;
public class UserEntity {
private Set<RoleEntity> roles = new HashSet<RoleEntity>();
public Set<RoleEntity> getRoles() {
return roles;
}
public void setRoles(Set<RoleEntity> roles) {
this.roles = roles;
}
}
Run Code Online (Sandbox Code Playgroud)
Roles类(注意equals和hashcode方法 ;如果删除它们,示例不再有效):
public class RoleEntity {
private long id;
private String name;
@Override
public int hashCode() {
return new Long(id).hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (! (obj instanceof RoleEntity)) {
return false;
}
return this.id == ((RoleEntity)obj).getId();
}
public RoleEntity(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
视图:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<form:form modelAttribute="userAttribute" method="POST" action="/something">
<table align="center">
<tr>
<td>ID</td>
<td>Role Name</td>
</tr>
<c:forEach items="${roleList}" var="role">
<tr>
<td><form:checkbox path="roles" value="${role}" label="${role.id}" /></td>
<td><c:out value="${role.name}" /></td>
</tr>
</c:forEach>
</table>
</form:form>
Run Code Online (Sandbox Code Playgroud)
PS只是关于你的JSP的一个观察.如果您value="${role}"使用表单:复选框,您将获得HTML复选框属性value="your.pack.age.declaration.RoleEntity@1",这些属性可能会让您在以后遇到其他麻烦.
| 归档时间: |
|
| 查看次数: |
45594 次 |
| 最近记录: |