
@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();
}
頁面傳參數menuIds , 數據庫保存用的參數是menuList, 中間通過setMenuIds--->setMenuIdList---->menuList
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)); } }
只貼出保存權限的,角色的省略,sql語句有點變態,很長很長,這里只是對生成的sql語句截取部分
<insert id="insertRoleMenu"> INSERT INTO sys_role_menu(role_id, menu_id) <foreach collection="menuList" item="menu" separator=" union all "> SELECT #{id}, #{menu.id} <if test="dbName != 'mssql'"> FROM dual </if> </foreach> </insert> ========================================== 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
節點的值在保存提交的時候存在這里,
<form:hidden path="menuIds"/>
<form:hidden path="officeIds"/>
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":
<collection property="officeList" ofType="Office">
<id property="id" column="officeList.id" />
</collection>

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>
用戶名重復校驗的比較簡潔好用。自己用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: "英文名已存在"}
},
