1: Why do I need a custom label?
When the JSP's built-in tags and tags in the JSTL tag library don't meet the requirements, developers are required to customize the label.
2: First introduce our web small configuration
<?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/javaee http://java.sun.co M/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.st Ruts.util.AuthorizeTag </tag-class> <body-content>JSP</body-content> <attribute> & Lt;name>url</name> <required>false</required> <RTEXPRVALUE>TRUE</RTEXPRVALUE&G T <type>java.lang.String</type> </attribute></tag></taglib>
3: Tool class
public class Authorizetag extends Bodytagsupport {//You provide a user name, I give a user a set of permissions, and the operation is in the right DAO private Iprivilegedao Privilegedao; Private String URL; Public String GetURL () {return URL; } public void SetUrl (String url) {url = URL; } @Override public int doStartTag () {//If the URL is not empty, the URL is displayed, otherwise the IF (null! = URL) {Getuserda O (); HttpServletRequest request = (HttpServletRequest) pagecontext.getrequest (); UserInfo info= (UserInfo) request.getsession (). getattribute ("UserInfo"); list<privilege> list = Privilegedao.fimdallprivilegebyuserid (Info.getuserid ()); for (Privilege item:list) {if (Item.geturl (). Equals (URL)) {//render the label correctly return eval_body_include; }}} return this. Skip_body; }public void Getuserdao () {Webapplicationcontext ApplicationContext = Webapplicationcontextutils.getwebapplicationcontext (Pagecontext.getservletcontext ()); privilegedao= (Iprivilegedao) ApplicationContext.Getbean ("Iprivilegedao");}
4:jsp
Introduce our web-customized links <% @taglib prefix= "Custom" uri= "http://www.springsecurity.org/jsp"%>// Click to get the table with the URL attribute in our column if his URL is not empty, display our "Add role" <custom:authorize url= "/role/addrole" ><a href= "#" > Add role </a></custom:authorize>
Web Custom Labels