在SpringMVC中不得不用到强大的数据绑定
MODEL:
PlatFormRole.java
@Entity @Table(name = "dsm_ROLE") public class PlatformRole extends DsmObject { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column private Integer role_id; @Column private String role_name; @Column private Integer role_class; @ManyToMany(mappedBy = "roles",fetch = FetchType.EAGER) private Set<PlatformUser> users; // 饥渴加载,在管理角色同时能够加载出相应的权限 @ManyToMany(fetch = FetchType.EAGER) @JoinTable(name = "dms_role_pri", joinColumns = { @JoinColumn(name = "rid") }, inverseJoinColumns = { @JoinColumn(name = "pid") }) private Set<PlatformPrivilege> pris; public Integer getRole_id() { return role_id; } public void setRole_id(Integer role_id) { this.role_id = role_id; } public String getRole_name() { return role_name; } public void setRole_name(String role_name) { this.role_name = role_name; } public Integer getRole_class() { return role_class; } public void setRole_class(Integer role_class) { this.role_class = role_class; } public PlatformRole(Integer role_id, String role_name, Integer role_class) { super(); this.role_id = role_id; this.role_name = role_name; this.role_class = role_class; } public PlatformRole() { } public Set<PlatformUser> getUsers() { return users; } public void setUsers(Set<PlatformUser> users) { this.users = users; } public Set<PlatformPrivilege> getPris() { return pris; } public void setPris(Set<PlatformPrivilege> pris) { this.pris = pris; } }
PlatformUser.java
public class PlatformUser extends DsmObject { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column private Integer userid; @Column private String username; @Column private String password; @Column private String work_id; @ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.EAGER) @JoinColumn(name = "org_id") private PlatformOrganization org_id; // 饥渴加载,在加载用户信息同时加载角色信息 @ManyToMany(fetch = FetchType.EAGER) @JoinTable(name = "dsm_user_role", joinColumns = { @JoinColumn(name = "userid") }, inverseJoinColumns = { @JoinColumn(name = "rid") }) private List<PlatformRole> roles; @Column private String telephone; public Integer getUserid() { return userid; } public void setUserid(Integer userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public List<PlatformRole> getRoles() { return roles; } public void setRoles(List<PlatformRole> roles) { this.roles = roles; } public String getWork_id() { return work_id; } public void setWork_id(String work_id) { this.work_id = work_id; } public PlatformOrganization getOrg_id() { return org_id; } public void setOrg_id(PlatformOrganization org_id) { this.org_id = org_id; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } public PlatformUser() { } }
可以看到,在PlatformUser中有一个角色属性,与用户的关系是一对多的
private List<PlatformRole> roles;
那么在修改某个用户的时候如何绑定当前用户的多个角色?
在Controller中:
@RequestMapping(value = "/{user_id}/update", method = RequestMethod.GET) public String update(@PathVariable int user_id, Model model) { model.addAttribute("platformOrganization", platformOrgService.findAllOrg()); model.addAttribute("user_id", user_id); List<PlatformRole> r = platformRoleService.findAllRole(); model.addAttribute("platformRole",r); model.addAttribute(platformUserService.findUserById(user_id)); return "/platform/updUser"; } @RequestMapping(value = "/{user_id}/update", method = RequestMethod.POST) public String update(@PathVariable int user_id, PlatformUser user) { platformUserService.updateUser(user); return "redirect:/platformIndex/users"; }
这是最常见的写法。
在View中:
<body> <div class="container"> <div class="row"> <div class=" col-md-12"> <div class="page-header"> <h2>用户修改</h2> </div> </div> </div> </div> <form:form action="platformIndex/${user_id}/update" method="post" modelAttribute="platformUser" class="form-horizontal" role="form"> <div class="form-group"> <label for="userid" class="col-md-2 control-label">用户id</label> <div class="col-md-6"> <form:input class="form-control" path="userid" placeholder="userid" /> </div> </div> <div class="form-group"> <label for="username" class="col-md-2 control-label">用户名</label> <div class="col-md-6"> <form:input class="form-control" path="username" placeholder="username" /> </div> </div> <div class="form-group"> <label for="password" class="col-md-2 control-label">密码</label> <div class="col-md-6"> <form:input class="form-control" path="password" placeholder="password" /> </div> </div> <div class="form-group"> <label for="role" class="col-md-2 control-label">角色</label> <div class="col-md-3"> <%-- <form:select path="role_id" class="form-control"> <form:option value="-" label="请选择" /> <form:options items="${platformRole}" itemValue="role_id" itemLabel="role_name" /> </form:select> --%> <%-- <form:select class="form-control" path="role_id.role_id" items="${platformRole}" itemValue="role_id" itemLabel="role_name" /> --%> <%-- <form:checkboxes path="roles" items="${platformRole}" itemValue="role_id" itemLabel="role_name" /> --%> <c:forEach items="${platformRole}" var="r" begin="0" varStatus="status"> <form:checkbox path="roles[${status.index }].role_id" value="${r.role_id }" label="${r.role_id }" /> </c:forEach> </div> </div> <div class="form-group"> <label for="workID" class="col-md-2 control-label">工号</label> <div class="col-md-6"> <form:input class="form-control" path="work_id" placeholder="workID" /> </div> </div> <div class="form-group"> <label for="organization" class="col-md-2 control-label">部门</label> <div class="col-md-3"> <form:select class="form-control" path="org_id.org_id" items="${platformOrganization}" itemValue="org_id" itemLabel="org_name" /> </div> </div> <div class="form-group"> <label for="telephone" class="col-md-2 control-label">联系方式</label> <div class="col-md-6"> <form:input class="form-control" path="telephone" placeholder="telephone" /> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <button type="submit" class="btn btn-default">修改</button> <button type="reset" class="btn btn-default">重置</button> </div> </div> </form:form> </body>
注意到角色的绑定:
<c:forEach items="${platformRole}" var="r" begin="0" varStatus="status"> <form:checkbox path="roles[${status.index }].role_id" value="${r.role_id }" label="${r.role_id }" /> </c:forEach>
通过这种方式可以讲User Model中的List<Role> roles;取出来
时间: 2024-10-14 21:01:06