First step: Create a TLD file under Web-inf
<?xml version="1.0"encoding="UTF-8"?> <taglib xmlns="Http://java.sun.com/xml/ns/javaee"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="Http://java.sun.com/xml/ns/javaeehttp//java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd "version="2.1"> <description> <! [Cdata[security tags]]> </description> <tlib-version>1.0</tlib-version> < Short-name>security</ Short-name> <uri>http://www.springsecurity.org/jsp</uri><tag> <description> <! [Cdata[authorize tag]]> </description> <name>authorize</name> <tag-class>Cn.tms.util.AuthorizeTag</tag-class> <body-content>JSP</body-content> <attribute> <name>url</nam E> <required>false</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> </tag> </taglib>
Authorize.tld
Step Two: Create a class to inherit Bodytagsupport
Package Cn.tms.util;import Java.util.list;import javax.servlet.http.httpservletrequest;import Javax.servlet.jsp.tagext.bodytagsupport;import Cn.tms.dao.iprivilegedao;import Cn.tms.dao.IRoleDAO;import Cn.tms.entity.privilege;import Cn.tms.entity.userinfo;import Org.springframework.web.context.webapplicationcontext;import Org.springframework.web.context.support.WebApplicationContextUtils;//label Public classAuthorizetag extends Bodytagsupport {//you provide a user name, I give a user a set of permissions, and the action is in the right DAO PrivateIprivilegedao Privilegedao; PrivateString URL; PublicString GetURL () {returnURL; } Public voidseturl (String url) {URL=URL; } @Override Public intdoStartTag () {//If the URL is not empty, the URL is displayed, otherwise it is not if(NULL!=URL) {Getuserdao (); HttpServletRequest Request=(HttpServletRequest) pagecontext.getrequest (); UserInfo Info= (UserInfo) request.getsession (). getattribute ("UserInfo"); List<Privilege> list =Privilegedao.findallprivilegebyuserid (Info.getuserid ()); System. out. println (List.size ()); for(Privilege item:list) {System. out. println (url+"=========================="); if(Item.geturl (). Equals (URL)) {//render the label correctly returnEval_body_include; } } } return This. Skip_body; } Public voidGetuserdao () {Webapplicationcontext ApplicationContext=Webapplicationcontextutils.getwebapplicationcontext (Pagecontext.getservletcontext ()); Privilegedao= (Iprivilegedao) Applicationcontext.getbean ("Iprivilegedao"); }}
Authorizetag
Step three: Add on the page before using it on the page
<% @taglib prefix= "Authorize" uri= "http://www.springsecurity.org/jsp"%>
* Note: The address of the URI here is consistent with the address in the Web-inf
Last use:
Authorize is your own definition here.
The URL is passed into the authorize, and then compared to the URL that was taken from the background, if the label is rendered consistently on the page, the label will not be rendered inconsistent
Jstl Custom Labels