纳税服务系统【角色与用户】
生活随笔
收集整理的這篇文章主要介紹了
纳税服务系统【角色与用户】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用戶與角色之間的關系
我們在做用戶模塊的時候,漏掉了最后一個功能。在新增功能中是可以選擇角色的。
用戶與角色之間的關系也是多對多
- 一個用戶對應多個角色
- 一個角色可以被多個用戶使用。
現在呢,我們的用戶表已經是寫的了。我們最好就不要修改原有的用戶表數據。那我們在不修改用戶表代碼的情況下,又怎么來實現多對多呢??
跟角色與權限是一樣的。使用中間表來維護它們的關系就行了。
用戶:user用戶id,名稱...1 用戶12 用戶2用戶角色:user_role用戶id,角色id1 11 22 2角色:role角色Id,名稱1 管理員2 一般用戶設計中間表
public class UserRole implements Serializable {private UserRoleId userRoleId;public UserRoleId getUserRoleId() {return userRoleId;}public void setUserRoleId(UserRoleId userRoleId) {this.userRoleId = userRoleId;} }主鍵表
public class UserRoleId implements Serializable {private String user_id;//在使用的時候,Role相關的數據會用得特別多。為了方便使用了Role對象。而user就不需要使用User對象了。private Role role;@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;UserRoleId that = (UserRoleId) o;if (user_id != null ? !user_id.equals(that.user_id) : that.user_id != null) return false;return role != null ? role.equals(that.role) : that.role == null;}@Overridepublic int hashCode() {int result = user_id != null ? user_id.hashCode() : 0;result = 31 * result + (role != null ? role.hashCode() : 0);return result;}public String getUser_id() {return user_id;}public void setUser_id(String user_id) {this.user_id = user_id;}public Role getRole() {return role;}public void setRole(Role role) {this.role = role;} }映射文件
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="zhongfucheng.user.entity.UserRole" table="user_role"><composite-id name="userRoleId" class="zhongfucheng.user.entity.UserRoleId"><!--manytoone可以生成外鍵字段。--><key-many-to-one name="role" class="zhongfucheng.role.entity.Role" column="role_id" lazy="false"/><key-property name="user_id" column="user_id" type="java.lang.String"/></composite-id></class></hibernate-mapping>增加模塊
在跳轉到JSP頁面的前,把所有的角色找出來。放到request域對象中,讓JSP頁面顯示出來。
public String addUI() {//把所有的角色查詢出來,帶過去給JSP頁面顯示ActionContext.getContext().getContextMap().put("roleList", roleServiceImpl.findObjects());return "addUI";} <%--list是集合對象name是要帶給服務器端的字符串數組。listkey 是集合元素對象的idlistValue 是集合元素對象的名字--%><s:checkboxlist list="#roleList" name="userRoleIds" listKey="roleId" listValue="name"/>編輯模塊
編輯回顯數據
在編輯模塊中,需要將該用戶所擁有的角色查詢出來。然后把查詢出來的id值放到數組中。
public String editUI() {//把所有的角色查詢出來,帶過去給JSP頁面顯示ActionContext.getContext().getContextMap().put("roleList", roleServiceImpl.findObjects());//外邊已經傳了id過來了,我們要找到id對應的Userif (user != null &&user.getId() != null ) {//直接獲取出來,后面JSP會根據User有getter就能讀取對應的信息!user = userServiceImpl.findObjectById(user.getId());//通過用戶的id得到所擁有UserRoleList<UserRole> roles = userServiceImpl.findRoleById(user.getId());//把用戶擁有角色的id填充到數組中,數組最后回顯到JSP頁面int i=0;userRoleIds = new String[roles.size()];for (UserRole role : roles) {userRoleIds[i++] = role.getUserRoleId().getRole().getRoleId();}}return "editUI";}JSP通過checkboxlist進行回顯,指定了name值就能夠自動判定我們的用戶擁有的角色是什么了。
<s:checkboxlist list="#roleList" name="userRoleIds" listKey="roleId" listValue="name"></s:checkboxlist>處理編輯操作
在更新之前,首先刪除用戶與角色之間的關系【歷史遺留問題】,如果不刪除,那么用戶所擁有的角色就一直保留著。無論你在JSP頁面有沒有勾選。
public String edit() throws IOException {//Struts2會自動把JSP帶過來的數據封裝到User對象上if (user.getId() != null && user != null) {if (headImg != null) {//得到要把頭像上傳到服務器的路徑javax.servlet.ServletContext servletContext = ServletActionContext.getServletContext();String realPath = servletContext.getRealPath("upload/user");//由于用戶上傳的名字可能會相同,如果相同就被覆蓋掉,因此我們要修改上傳文件的名字【獨一無二】headImgFileName = UUID.randomUUID().toString() + headImgFileName.substring(headImgFileName.lastIndexOf("."));FileUtils.copyFile(headImg, new File(realPath, headImgFileName));//設置圖片與用戶的關系user.setHeadImg(headImgFileName);}if (userRoleIds != null) {//刪除用戶與角色之間的關系【歷史遺留問題】userServiceImpl.deleteUserRoleById(userRoleIds);//保存用戶與角色。userServiceImpl.saveUserAndRole(user,userRoleIds);}}return "list";}調用保存用戶與角色的關系。如果id不是為空的,那么就執行更新,如果id為空,就執行保存。
@Overridepublic void saveUserAndRole(User user, String... userRoleIds) {//保存或更新用戶if (user.getId() != null) {userDaoImpl.update(user);} else {userDaoImpl.save(user);}//判斷有沒有把id帶過來if (userRoleIds != null) {for (String userRoleId : userRoleIds) {System.out.println(userRoleId);userDaoImpl.saveUserRole(new UserRole(new UserRoleId(user.getId(), new Role(userRoleId))));}}}轉載于:https://www.cnblogs.com/zhong-fucheng/p/7202917.html
總結
以上是生活随笔為你收集整理的纳税服务系统【角色与用户】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VBS 自动发送邮件
- 下一篇: java信息管理系统总结_java实现科