
@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: "英文名已存在"}
},