1. 权限校验action
public class CheckPrivilegeAction extends Action{ public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) { PrivilegeForm pf = (PrivilegeForm) form; response.setContentType("text/json;charset=utf-8"); List<String> urls = new ArrayList<String>(); JSONArray json = null; try { urls = JMongoDB.getUserPrivilegeList(pf.getUid(),true); json = JSONArray.fromObject(urls); PrintWriter out = response.getWriter(); System.out.println(json); out.print(json); out.flush(); } catch (Exception e) { e.printStackTrace(); } return null; } }
2. 权限管理页面
<%@page import="com.db.po.Privilege"%> <%@ page language="java" contentType="text/html; charset=gbk"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@page import="com.db.po.User"%> <%@page import="java.util.List"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <html> <head> <title>JSP for UserForm form</title> <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script> <script type="text/javascript"> $(function() { $("#uid").click(function() { var params = { uid : $("#uid").val() }; $.ajax({ url : "checkPrivilege.do",data : params,type : 'post',dataType : 'json',success : function(data) { // 获取权限集合,在checkBox里显示 var inputs = document.getElementsByTagName("input"); // 显示其它用户权限checkBox前,先清空当前checkBox已选 for ( var i = 0; i < inputs.length; i++) { var o = inputs[i]; if (o.type == "checkBox") { o.checked = ""; } } // 为当前用户权限checkBox设置已选 for ( var i = 0; i < inputs.length; i++) { var obj = inputs[i]; if (obj.type == "checkBox") { for ( var j = 0; j < data.length; j++) { //alert("当前权限为:" + obj.value); if (data[j] == obj.value) { //alert("设置已选"); obj.checked = "checked"; } } } } },error : function() { alert("获取权限列表集合失败!"); } }); }); }); </script> </head> <body> <% User user1 = (User) session.getAttribute("user"); session.setAttribute("user",user1); if (user1 != null) { List<User> users = (List<User>) request.getAttribute("users"); List<Privilege> privileges = (List<Privilege>) request .getAttribute("privileges"); %> <form action="${pageContext.request.contextPath }/addPrivilege.do"> <table align="center" width="80%" border="1" style="border-collapse: collapse"> <caption> <h2>权限管理</h2> </caption> <tr> <td>用户列表</td> <td>权限列表</td> </tr> <tr> <td><select id="uid" name="uid"> <% for (User user : users) { out.print("<option>" + user.getId() + "</option>"); } %> </select></td> <td> <% for (Privilege p : privileges) { %> <input type="checkBox" name="url" value="<%=p.getUrl()%>" id="url" /><%=p.getPriName()%> <% } %> </td> </tr> <tr> <td><html:submit>提交</html:submit> </td> <td><html:reset>重置</html:reset> </td> </tr> </table> </form> <% } else { response.sendRedirect(request.getContextPath() + "/login.jsp"); } %> <div></div> </body> </html>