前言
如何自定义hasAnyPermission 标签
实现hasAnyPermission
- 实现
public class HasAnyPermssionTag extends PermissionTag {public HasAnyPermssionTag() {}protected boolean showTagBody(String p) {String[] arr = p.replaceAll("\r\n", "").replaceAll(" ", "").split(",");if (arr.length > 0) {for (String s : arr) {boolean flag = this.isPermitted(s);if (flag) {return true;}}}return false;}
}
- 创建shiroExt.tld文件,这个文件可以参考shiro.tld文件,此文件放在web.xml同级或者下一级即可。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN""http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"><taglib><tlib-version>1.1.2</tlib-version><jsp-version>1.2</jsp-version><short-name>Apache Shiro</short-name><uri>/myshiro</uri><description>Apache Shiro JSP Tag Library.</description><tag><name>hasAnyPermssion</name><tag-class>com.example.mytest01.util.HasAnyPermssionTag</tag-class><body-content>JSP</body-content><description>拥有其中任意一个权限即可</description><attribute><name>name</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute></tag>
</taglib>
- 配置到web.xml中
<jsp-config><taglib><taglib-uri>/myshiro</taglib-uri><taglib-location>/WEB-INF/tags/shiroExt.tld</taglib-location></taglib>
</jsp-config>
- 在jsp页面中使用这个标签
引入扩展标签和是使用
<%@ taglib uri="/myshiro" prefix="shiroExt" %><shiroExt:hasAnyPermssion name="hr:manage:view,it:manage:view">
<a href>查看</a>
</shiroExt:hasAnyPermssion>