JSP custom tag Development

Source: Internet
Author: User
Tags tld

Generally, the following two packages must be referenced to develop jsp custom tags:

ImportJavax. servlet. jsp .*;

ImportJavax. servlet. jsp. tagext .*;

First, we need to get a general idea of the interface and class hierarchy involved in the development of custom tags (the SimpleTag interface and SimpleTagSupport class are newly introduced in JSP2.0 ).

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Authorization + itcS88rWlserHqTwvaDE + CjxwPgogPC9wPgo8cD4K0Ke5 + 828o7o8L3A + CjxwPgo8aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20140214/2014021408420654.png" alt = "\">

Use this custom tag on the jsp page:

Assume that we have a UserInfo javabean, you only need to call this tag to use this tag on the JSP page.

<%

UserInfo user =NewUserInfo ();

User. setUserName ("Xuwei ");

User. setAge (33 );

User. setEmail ("test@test.test ");

PageContext. setAttribute ("userinfo", user );

%>

"$ {Pagination. userinfo} "/>

Development steps:

For simple Tag development, we only need to implement the Tag interface. For simplicity, we can directly inherit the TagSupport class that implements this interface.

1. Create a custom label class

Public classUserInfoTagExtendsTagSupport {

PrivateUserInfo user;

@ Override

Public intDoStartTag ()ThrowsJspException {

Try{

JspWriter out =This. PageContext. getOut ();

If(User =Null){

Out. println ("No UserInfo Found ...");

Return SKIP_BODY;

}

Out. println ("

Out. println ("

Out. println ("

Out. println ("

Out. println ("

Out. println ("

Out. println ("

Out. println ("

Out. println ("

Out. println ("

Out. println ("

Out. println ("

Out. println ("

Out. println ("

"); "); "); "); "); "); "); "); "); "); "); "); ");
Username:"+ User. getUserName () +"
Age:"+ User. getAge () +"
Email:"+ User. getEmail () +"
");

}Catch(Exception e ){

Throw newJspException (e. getMessage ());

}

Return SKIP_BODY;

}

@ Override

Public intDoEndTag ()ThrowsJspException {

Return EVAL_PAGE;

}

@ Override

Public voidRelease (){

Super. Release ();

This. User =Null;

}

// Getter and setters

PublicUserInfo getUser (){

ReturnUser;

}

Public voidSetUser (UserInfo user ){

This. User = user;

}

}

2. Create the Tag Library Description file in Web-Inf. tdl (Tag Library Description)

"1.0"Encoding =UTF-8"?>

"2.0"Xmlns = Http://java.sun.com/xml/ns/j2ee"

Xmlns: xsi =Http://www.w3.org/2001/XMLSchema-instance"

Xsi: schemaLocation =Http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">

1.0

2.0

Cc

/Mytaglib

ShowUserInfo

Com. mytags. UserInfoTag

Empty

User

False

True

3. Configure web. xml

/Mytaglib

/WEB-INF/mytaglib. tld

4. Import it in the header of the jsp page that requires this tag

<% @ Taglib uri = "/mytaglib" prefix = "cc" %>

5. Use (refer to the above steps)

Therefore, a simple JSP tag is developed

Label description:

The UserInfoTag class we created inherits the TagSupport class and implements the Tag interface. the lifecycle of the Tag interface is controlled by the container where it is located, for example:

SetPageContext () injects the pageContext of the jsp page to access the pageContext attribute of the jsp page object in subsequent methods.

SetParent () sets the parent tag of this tag

SetAttribute () injects the attributes in the tag into the attributes of this class. You do not need to implement it yourself, but you need to provide the get and set methods of the attributes.

DoStartTag () is called after tag attribute setting. If SKIP_BODY is returned, content in the tag is ignored. If EVAL_BODY_INCLUDE is returned, the content of the TAG body is output.

The doEndTag () is called before the end tag. The SKIP_PAGE is returned to skip the output after the entire jsp page, and the remaining part of the EVAL_PAGE execution page is returned.

Called at the end of the release () Lifecycle

Note: The Tag Buffer Pool is enabled by default in Versions later than tomcat4.1 (this is not the case for websphere and weblogic), so the release () is not executed after the tag is executed () method (_ jspDestroy (), that is, the same jsp page custom tag will only exist for one instance no matter how many times it is used, however, not every tag creates a buffer pool for it, which must be determined by parameters. For example:

In the preceding example, two Tag Buffer pools are created due to different parameters.

You can solve this problem by setting the tomcat configuration file:
Add the enablePooling parameter to % tomcat % \ conf \ web. xml and set it to false (do not cache custom labels ).


EnablePooling
False

Clear % tomcat % \ conf \ directory

Bytes -------------------------------------------------------------------------------------------------------------------------------

The TagSupport class has implemented and extended some methods for US (for example, in the above method, we can directly use the pageContext object and call the parent tag getParent ), in general, we only need to rewrite doStartTag () and doEndTag ().

TLD file description:

1.0

2.0

Cc

ShowUserInfo

Com. mytags. UserInfoTag

Empty

User

False

True

Web. xml file description:

/Mytaglib

/WEB-INF/mytaglib. tld


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.