今日用到了DWR让我折腾了一会,最终搞定,先记住,方便下次不用麻烦!
1,添加Dwr.jar
2,配置web.xml(请自行添加ContextLoaderListener监听器,否则bean无法获取!),
<servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
3,配置dwr.xml(将此文件添加到WEB-INFO目录下,此配置方法很多,详参考相关文档)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd"> <dwr> <allow> <create creator="spring" javascript="aclManager" scope="application"> <param name="beanName" value="aclManager"/> </create> <convert converter="bean" match="com.panlong.service.impl.AclManagerImpl" /> </allow> </dwr>
4,要使用时请先在调用页面引入文件如下:
<script type="text/javascript" src="<%=request.getContextPath() %>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/dwr/util.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/dwr/interface/aclManager.js"></script>
5,使用例子。
<script type="text/javascript"> //授权 function addOrUpdatePermission(field){ dwr.engine.setAsync(false); //如果被选择上,则同时选择其"不继承"和"启用"checkbox if(field.checked){ $(field.moduleId+"_USE").checked = true; <c:if test="${aclInfo.principalType eq 'User' }"> $(field.moduleId+"_EXT").checked = true; addOrUpdateExtends(field); </c:if> } aclManager.addOrUpdatePermission( "${aclInfo.principalType}", ${aclInfo.principalId}, field.moduleId, field.permission, field.checked ); } //设置用户的继承特性 function addOrUpdateExtends(field){ aclManager.addOrUpdateUserExtends( ${aclInfo.principalId}, field.moduleId, !field.checked ); } //点击启用checkbox function usePermission(field){ //如果checkbox被选中,意味着需要更新ACL的状态 //更新C/R/U/D以及Extends状态 //设置为同步方式,以便DWR依次发出下列请求 dwr.engine.setAsync(false); if(field.checked){ addOrUpdatePermission($(field.moduleId+"_C")); addOrUpdatePermission($(field.moduleId+"_R")); addOrUpdatePermission($(field.moduleId+"_U")); addOrUpdatePermission($(field.moduleId+"_D")); <c:if test="${aclInfo.principalType eq 'User' }"> addOrUpdateExtends($(field.moduleId+"_EXT")); </c:if> }else{ aclManager.delPermission( "${aclInfo.principalType}", ${aclInfo.principalId}, field.moduleId ); $(field.moduleId+"_C").checked = false; $(field.moduleId+"_R").checked = false; $(field.moduleId+"_U").checked = false; $(field.moduleId+"_D").checked = false; <c:if test="${aclInfo.principalType eq 'User' }"> $(field.moduleId+"_EXT").checked = false; </c:if> } } function initTable(){ aclManager.searchAclRecord( "${aclInfo.principalType}", ${aclInfo.principalId}, function(datas){ for(var i=0; i < datas.length; i++){ var moduleId = datas[i][0]; var cState = datas[i][1]; var rState = datas[i][2]; var uState = datas[i][3]; var dState = datas[i][4]; var extState = datas[i][5]; $(moduleId+"_C").checked = cState == 0 ? false : true; $(moduleId+"_R").checked = rState == 0 ? false : true; $(moduleId+"_U").checked = uState == 0 ? false : true; $(moduleId+"_D").checked = dState == 0 ? false : true; <c:if test="${aclInfo.principalType eq 'User' }"> $(moduleId+"_EXT").checked = extState == 0 ? true : false; </c:if> $(moduleId+"_USE").checked = true; } } ); } </script>
结束!