@RequiresPermissions("sys:role:edit") @RequestMapping(value = "save") public String save(Role role, Model model, RedirectAttributes redirectAttributes) { if(!UserUtils.getUser().isAdmin()&&role.getSysData().equals(Global.YES)){ addMessage(redirectAttributes, "越權操做,只有超級管理員才能修改此數據!"); return "redirect:" + adminPath + "/sys/role/?repage"; } if(Global.isDemoMode()){ addMessage(redirectAttributes, "演示模式,不容許操做!"); return "redirect:" + adminPath + "/sys/role/?repage"; } if (!beanValidator(model, role)){ return form(role, model); } if (!"true".equals(checkName(role.getOldName(), role.getName()))){ addMessage(model, "保存角色'" + role.getName() + "'失敗, 角色名已存在"); return form(role, model); } if (!"true".equals(checkEnname(role.getOldEnname(), role.getEnname()))){ addMessage(model, "保存角色'" + role.getName() + "'失敗, 英文名已存在"); return form(role, model); } systemService.saveRole(role); addMessage(redirectAttributes, "保存角色'" + role.getName() + "'成功"); return "redirect:" + adminPath + "/sys/role/?repage"; } ======================================== @Transactional(readOnly = false) public void saveRole(Role role) { if (StringUtils.isBlank(role.getId())){ role.preInsert(); roleDao.insert(role); // 同步到Activiti saveActivitiGroup(role); }else{ role.preUpdate(); roleDao.update(role); } // 更新角色與菜單關聯 roleDao.deleteRoleMenu(role); if (role.getMenuList().size() > 0){ roleDao.insertRoleMenu(role); } // 更新角色與部門關聯 roleDao.deleteRoleOffice(role); if (role.getOfficeList().size() > 0){ roleDao.insertRoleOffice(role); } // 同步到Activiti saveActivitiGroup(role); // 清除用戶角色緩存 UserUtils.removeCache(UserUtils.CACHE_ROLE_LIST); // // 清除權限緩存 // systemRealm.clearAllCachedAuthorizationInfo(); }
========================================java
頁面傳參數menuIds , 數據庫保存用的參數是menuList, 中間經過setMenuIds--->setMenuIdList---->menuListnode
public List<String> getMenuIdList() { List<String> menuIdList = Lists.newArrayList(); for (Menu menu : menuList) { menuIdList.add(menu.getId()); } return menuIdList; } public void setMenuIdList(List<String> menuIdList) { menuList = Lists.newArrayList(); for (String menuId : menuIdList) { Menu menu = new Menu(); menu.setId(menuId); menuList.add(menu); } } public String getMenuIds() { return StringUtils.join(getMenuIdList(), ","); } public void setMenuIds(String menuIds) { menuList = Lists.newArrayList(); if (menuIds != null){ String[] ids = StringUtils.split(menuIds, ","); setMenuIdList(Lists.newArrayList(ids)); } } role.java
只貼出保存權限的,角色的省略,sql語句有點變態,很長很長,這裏只是對生成的sql語句截取部分ajax
INSERT INTO sys_role_menu(role_id, menu_id) SELECT #{id}, #{menu.id} FROM dual ========================================== INSERT INTO sys_role_menu(role_id, menu_id) SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual
節點的值在保存提交的時候存在這裏,sql
submitHandler: function(form){ var ids = [], nodes = tree.getCheckedNodes(true); for(var i=0; i<nodes.length; i++) { ids.push(nodes[i].id); } $("#menuIds").val(ids); var ids2 = [], nodes2 = tree2.getCheckedNodes(true); for(var i=0; i<nodes2.length; i++) { ids2.push(nodes2[i].id); } $("#officeIds").val(ids2); loading('正在提交,請稍等…'); form.submit(); },
有個地方注意一點,在roleDao.xml 的get 方法中,column="officeList.id" , 由於後面命名了 ro.office_id AS "officeList.id":數據庫
form:select 標籤挺好用的,還帶搜索篩選功能緩存
form:select path="roleType" class="input-medium"> <form:option value="assignment">任務分配</form:option> <form:option value="security-role">管理角色</form:option> <form:option value="user">普通角色</form:option> </form:select>
任務分配 管理角色 普通角色app
用戶名重複校驗的比較簡潔好用。本身用ajax異步請求也行,可是寫的代碼會比較多。異步
$("#inputForm").validate({ rules: { name: {remote: "${ctx}/sys/role/checkName?oldName=" + encodeURIComponent("${role.name}")}, enname: {remote: "${ctx}/sys/role/checkEnname?oldEnname=" + encodeURIComponent("${role.enname}")} }, messages: { name: {remote: "角色名已存在"}, enname: {remote: "英文名已存在"} },