@RequestMapping(value = "/add", method=RequestMethod.GET)
<span style="white-space:pre"> </span>public String add(ModelMap model) {
<span style="white-space:pre"> </span>List<Authority> auths = authorityService.selectAllAuthority();
<span style="white-space:pre"> </span>model.addAttribute("auths", auths);
<span style="white-space:pre"> </span>return "manage/role/add";
<span style="white-space:pre"> </span>}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:include page="../common/top.jsp" />
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.cmcc.check.model.Role"%>
<%@page import="com.cmcc.check.model.Authority"%>
<form action="<%=request.getContextPath()%>/manage/tab1/role/add.html" method="post" class="definewidth m20">
<table class="table table-bordered table-hover definewidth m10">
<tr>
<td width="10%" class="tableleft">角色名称</td>
<td><input type="text" name="name" maxlength="20"/></td>
</tr>
<tr>
<td class="tableleft">状态</td>
<td>
<input type="radio" name="roleStatus" value="0" checked/>启用
<input type="radio" name="roleStatus" value="1"/>禁用
</td>
</tr>
<tr>
<td class="tableleft">权限</td>
<td>
<ul>
<%
List<Authority> auths = (List<Authority>) request.getAttribute("auths");
Authority auth = null;
if (auths != null) {
for (int i=0; i<auths.size(); i++) {
auth = auths.get(i);
%>
<li>
<label class="checkbox inline"><input type="checkbox" name="authIds" value="<%=auth.getAuthorityId()%>"><%=auth.getName()%></label>
</li>
<%
}
}
%>
</ul>
</td>
</tr>
<tr>
<td class="tableleft"></td>
<td>
<button type="submit" class="btn btn-primary" type="button">保存</button>
<button type="button" class="btn btn-success" name="backid" id="backid">返回列表</button>
<%
String message = (String) request.getAttribute("message");
if(message != null)
out.write("<span style='color:red'>"+message+"</span>");
%>
</td>
</tr>
</table>
</form>
<script>
$(function () {
$(':checkbox[name="group[]"]').click(function () {
$(':checkbox', $(this).closest('li')).prop('checked', this.checked);
});
$('#backid').click(function(){
window.location.href="<%=request.getContextPath()%>/manage/tab1/role/index.html";
});
});
</script>
<jsp:include page="../common/bottom.jsp" />
/**
* 添加提交
*/
@RequestMapping(value = "/add", method=RequestMethod.POST)
public String addSubmit(Role role, Long[] authIds, HttpServletRequest req, ModelMap model){
if(role == null || StringUtils.isBlank(role.getName())){
model.addAttribute("message", "输入参数不全");
return add(model);
}
Long userid = (Long)req.getSession().getAttribute("userid");
//TODO 获取当前操作人
role.setCreator(userid == null ? 0L : userid);
roleService.saveRoleObj(role, authIds);
return "redirect:/manage/tab1/role/index.html";
}
/**
* 编辑角色
*/
@RequestMapping(value="/modify", method=RequestMethod.GET)
public String modify(Long roleId, ModelMap model){
Role role = roleService.getRoleById(roleId);
Set<Long> authIds = roleAuthorityService.getAuthsByRole(roleId);
List<Authority> auths = authorityService.selectAllAuthority();
model.addAttribute("role", role);
model.addAttribute("authIds", authIds);
model.addAttribute("auths", auths);
return "manage/role/modify";
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:include page="../common/top.jsp" />
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Set"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.cmcc.check.model.Role"%>
<%@page import="com.cmcc.check.model.Authority"%>
<%@page import="com.cmcc.check.model.RoleAuthorityKey"%>
<%
Role role = (Role)request.getAttribute("role");
%>
<form action="<%=request.getContextPath()%>/manage/tab1/role/update.html" method="post" class="definewidth m20">
<input type="hidden" name="roleId" value="<%=role.getRoleId()%>"/>
<table class="table table-bordered table-hover definewidth m10">
<tr>
<td width="10%" class="tableleft">角色名称</td>
<td><input type="text" name="name" maxlength="20" value="<%=role.getName()%>"/></td>
</tr>
<tr>
<td class="tableleft">状态</td>
<td>
<input type="radio" name="roleStatus" value="0" <%if(role.getRoleStatus() == 0){%> checked <%} %>/>启用
<input type="radio" name="roleStatus" value="1" <%if(role.getRoleStatus() == 1){%> checked <%} %>/>禁用
</td>
</tr>
<tr>
<td class="tableleft">权限</td>
<td>
<ul>
<%
List<Authority> auths = (List<Authority>) request.getAttribute("auths");
Authority auth = null;
if (auths != null) {
Set<Long> authIds = (Set<Long>) request.getAttribute("authIds");
for (int i=0; i<auths.size(); i++) {
auth = auths.get(i);
%>
<li>
<label class="checkbox inline"><input type="checkbox" <%if(authIds.contains(auth.getAuthorityId())){ %> checked <%} %> name="authIds" value="<%=auth.getAuthorityId()%>"><%=auth.getName()%></label>
</li>
<%
}
}
%>
</ul>
</td>
</tr>
<tr>
<td class="tableleft"></td>
<td>
<button type="submit" class="btn btn-primary" type="button">修改</button>
<button type="button" class="btn btn-success" name="backid" id="backid">返回列表</button>
<%
String message = (String) request.getAttribute("message");
if(message != null)
out.write("<span style='color:red'>"+message+"</span>");
%>
</td>
</tr>
</table>
</form>
<script>
$(function () {
$(':checkbox[name="group[]"]').click(function () {
$(':checkbox', $(this).closest('li')).prop('checked', this.checked);
});
$('#backid').click(function(){
window.location.href="<%=request.getContextPath()%>/manage/tab1/role/index.html";
});
});
</script>
<jsp:include page="../common/bottom.jsp" />
/**
* 修改保存角色
*/
@RequestMapping(value = "/update", method=RequestMethod.POST)
public String update(Role role, Long[] authIds, HttpServletRequest req, ModelMap model){
if(role == null || role.getRoleId() == null)
return "redirect:/manage/tab1/role/index.html";
if(StringUtils.isBlank(role.getName())){
model.addAttribute("message", "输入参数不全");
return modify(role.getRoleId(), model);
}
Long userid = (Long)req.getSession().getAttribute("userid");
//TODO 获取当前操作人
role.setUpdateUser(userid == null ? 0L : userid);
Set<Long> oldAuthIds = roleAuthorityService.getAuthsByRole(role.getRoleId());
roleService.updateRoleObj(role, authIds, oldAuthIds);
return "redirect:/manage/tab1/role/index.html";
}
批量插入数据到数据库:
<!-- 批量插入 -->
<insert id="insertBatch" parameterType="java.util.List">
insert into role_auth (role_id, auth_id)
values
<foreach collection="list" item="item" index="index" separator=",">
(${item.roleId}, ${item.authId})
</foreach>
</insert>