Javabean
PlatformUser.java
@Entity @Table(name = "dsm_USER") 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 Set<PlatformRole> roles = new HashSet<>(); @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 Set<PlatformRole> getRoles() { return roles; } public void setRoles(Set<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() { } }
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 = new HashSet<>(); // 饥渴加载,在管理角色同时能够加载出相应的权限 @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; } }
User与Role是多对多的关系。
在更新某一个用户的同时,将用户的多个角色信息绑定到Checkbox上
Controller:
@InitBinder public void initBinder(WebDataBinder binder) throws Exception { binder.registerCustomEditor(Set.class, new PropertyEditorSupport() { @Override public void setAsText(String text) { System.out.println(text); // Long systemInfoId = Long.valueOf(text); // SystemInfo systemInfo = // systemInfoService.findById(systemInfoId); String[] roles = text.split(","); Set<PlatformRole> platformRoles = new HashSet<>(); PlatformRole platformRole = null; for (int i = 0; i < roles.length; i++) { platformRole = platformRoleService.findRoleById(Integer .parseInt(roles[i])); platformRoles.add(platformRole); } System.out.println(platformRole); setValue(platformRoles); } }); } /*********** updUser **************/ @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); PlatformUser u = platformUserService.findUserById(user_id); Set<PlatformRole> us = u.getRoles(); model.addAttribute(u); List<PlatformRole> r = platformRoleService.findAllRole(); /* * for(int i=0;i<r.size();i++){ if(r.get(i).getRole_id().equals(us.)) } */ Iterator<PlatformRole> it = us.iterator(); while (it.hasNext()) { PlatformRole po = it.next(); int itu = po.getRole_id(); for (int i = 0; i < r.size(); i++) { if (r.get(i).getRole_id().equals(itu)) { r.set(i, po); } } } model.addAttribute("platformRole", r); 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"; }
在updateUser.jsp中:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>manage</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <link rel="stylesheet" type="text/css" href="resources/bootstrap.css"> <script src="resources/jquery-1.7.1.js"></script> <script src="resources/bootstrap.min.js"></script> </head> <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="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="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_name }" /> </c:forEach> <form:checkboxes path="roles" items="${platformRole}" itemValue="role_id" itemLabel="role_name" /> <c:forEach items="${platformRole }" var="r"> <form:checkbox path="roles" label="${r.role_name }" value="${r.role_id }" /> </c:forEach> <form:checkboxes path="roles" items="${platformRole}" itemValue="role_id" itemLabel="role_name" /> <c:forEach items="${platformRole}" var="role" varStatus="status"> <form:checkbox path="roles" value="${role}" label="${role.role_name}" /> </c:forEach> --%> <form:checkboxes path="roles" items="${platformRole}" itemValue="role_id" itemLabel="role_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> </html>
时间: 2024-11-13 07:52:37