Jsp custom tag

Source: Internet
Author: User
Tags tld

Jsp custom tag

Recently, the project was changed to jsp for all the original static pages. The manager asked me to write a custom jsp tag to handle permission issues, after writing it out, I found that using custom tags to control permissions is much more convenient than the tedious judgment in js on static pages. So I recorded it for later use, the specific method was found in a blog of an unknown friend. Because I searched a lot and was busy with projects and didn't reserve the address, I won't leave the portal here.

First, you need to write a tld file, placed under the WEB-INF, to define the label name, attributes, and background processing class.

 

 
 
  
  
   
A tag library exercising SimpleTag handlers.
  
  
   
1.0
  
  
   
Crb /IsHasPermission JSP Self-define Tag Library. This is an authority tag
    
HasPermission Com. zzvc. crb. tag. HasPermission JSP Name False True

 

 

 

 

Where The label attribute is variable. Here, bodycontent has three optional values.
1. jsp: the TAG body is composed of other jsp elements. If there is a jsp element, the tag will be explained first, and the actual value of the element will be passed in. For example, if the TAG body contains jsp elements such as <% = attributeName %>, the tag will be passed in according to the actual value of attributeName. This is the most common one.
2. empty: the Tag body must be empty. When referencing this Tag, you can Instead of having
3. tagdependent: interpreted by tags without jsp Conversion

2. You need to configure the tag in web. xml,

   
    
     
      /crb
     
     
      /WEB-INF/roletag.tld
     
    
   
Iii. Tag Processing

 

 

Public class HasPermission extends TagSupport {private String name; public int doStartTag () throws JspException {HttpSession session = pageContext. getSession (); Person person = (Person) session. getAttribute ("person"); if (person. getAuthorities (). contains (name) {return EVAL_BODY_INCLUDE; // normal execution} else {return SKIP_BODY; // The 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:

 

1. This class inherits TagSupport;

2. the attribute of a tag must be written as a member variable and the set and get methods must be claimed.

 

So far, this simple custom tag has been written and can be referenced in jsp:

 

<%@ taglib prefix="crb" uri="/isHasPermission" %>

   
You can write jsp code in the middle of the tag. If the current user has the permission for the corresponding name value, the jsp code is executed normally. Otherwise, the jsp code is hidden and not visible to the user.

 

This is just a simple custom tag. to implement more complex functions, you only need to enrich the logic processing of the processing class, which is indeed very convenient.

Related Article

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.