Jstl Custom Labels

Source: Internet
Author: User
Tags cdata tld

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.