JSP Custom Labels

Source: Internet
Author: User
Tags tag name tld

Recent Project big change, original static page to all changed to JSP, manager let me write a custom JSP tag, deal with permissions problem, do not know, write out after found to use custom Label method control permission, than static page in JS in the cumbersome judgment convenient a lot, so record a bit, to stay after use, The concrete method is in an unknown friend's blog to see, because at that time a lot of search, busy project, no reservation address, here will not put the portal.

First, to write a TLD file, placed under Web-inf, to define the name of this tag, attributes, and the processing class in the background.

<?xml version= "1.0" encoding= "Utf-8"? ><taglib xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "http// Www.w3.org/2001/XMLSchema-instance "xsi:schemallocation=" Http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0. xsd "version=" 2.0 "><!--tag Description--><description>a tag Library exercising Simpletag handlers.</ Description><tlib-version>1.0</tlib-version><pre name= "code" class= "HTML" ><span style= " White-space:pre "></span><!--label----
<short-name>crb</short-name><uri>/ishaspermission</uri><description>jsp Self-define Tag Library.</description><tag><description>this is an authority tag</description >
<span style= "White-space:pre" ></span><!--tag Name--
<name>hasPermission</name><!--Processing Class--><tag-class>com.zzvc.crb.tag.haspermission</ tag-class><!--Set how the contents of the label are handled--><body-content>jsp</body-content><!--label Properties-->< attribute><name>name</name><required>false</required><rtexprvalue>true</ Rtexprvalue></attribute></tag></taglib>


Where the <body-content> tag properties are variable, here Bodycontent has three optional values
1. JSP: The tag body consists of other JSP elements, if it has a JSP element, then the label will be interpreted first, and then the actual value of the element passed in. For example, the tag body contains a JSP element such as <%=attributeName%>, at this time the label will press AttributeName the actual value of what will pass in what. This is the most common one.
2. Empty: The label body must be empty, in the reference to this tag, you can <crb:haspermission name= "Person_read"/&GT;crb:haspermissionname="Person_read"></crb:haspermission>
3. Tagdependent: Interpreted by tags, without JSP conversion

After that, the tag needs to be configured in Web. Xml.

<jsp-config><taglib><taglib-uri>/crb</taglib-uri><span style= "White-space:pre" > </span><taglib-location>/web-inf/roletag.tld</taglib-location></taglib></jsp-config >
Third, write the label processing class

public class Haspermission extends TagSupport {private String name;public int doStartTag () throws Jspexception {Httpsessio n session = Pagecontext.getsession (); Person person = (person) session.getattribute ("person"), if (Person.getauthorities (). Contains (name)) {return eval_body _include; Normal execution} else {return skip_body;//content is ignored}}public int Doendtag () throws jspexception {return eval_page;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}

Note two points:

1. This class inherits TagSupport;

2. The properties of the tag need to be written as member variables and claim the set and get methods.


At this point, this simple custom label is written and can be referenced in the JSP:

<%@ taglib prefix= "CRB" uri= "/ishaspermission"%>

<crb:haspermission name= "Person_read" ></crb:hasPermission>
The JSP code can be written in the middle of the label, if the current user has the name value corresponding permissions, the JSP code is normal execution, and vice versa, the segment JSP code is hidden, not seen by the user.

This is just a simple custom label, if you need to implement more complex functions, generally only need to enrich the processing of the logic of the class can be, indeed very convenient.

JSP 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.