Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get started with JSP Custom tag Development

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to get started with JSP custom tag development. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

In general, to develop jsp custom tags, you need to reference the following two packages

Import javax.servlet.jsp.*

Import javax.servlet.jsp.tagext.*

First of all, we need to get an overview of the hierarchy of interfaces and classes involved in developing custom tags (where the SimpleTag interface and SimpleTagSupport classes are new to JSP2.0).

Goal 1: customize a simple label that displays user information in a table

Effect picture:

Use this custom tag on the jsp page:

Suppose we have a javabean for UserInfo, so we only need to call this tag to use this tag on the JSP page.

Development steps:

For the development of simple tags, 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 class UserInfoTag extends TagSupport {private UserInfo user; @ Override public int doStartTag () throws JspException {try {JspWriter out = this.pageContext.getOut (); if (user = = null) {out.println ("No UserInfo Found..."); return SKIP_BODY;} out.println ("") Out.println (""); out.println ("Username:"); out.println ("+ user.getUserName () +"); out.println (""); out.println (""); out.println ("Age:"); out.println ("" + user.getAge () + ") Out.println ("); out.println ("); out.println ("Email:"); out.println ("+ user.getEmail () +"); out.println (""); out.println ("");} catch (Exception e) {throw new JspException (e.getMessage ()) } return SKIP_BODY;} @ Override public int doEndTag () throws JspException {return EVAL_PAGE;} @ Override public void release () {super.release (); this.user = null;} / / getter and setters public UserInfo getUser () {return user } public void setUser (UserInfo user) {this.user = user;}}

2 create tag library description file .tdl (Tag Library Description) in Web-Inf

1.0 2.0 cc / mytaglib showUserInfo com.mytags.UserInfoTag empty user false true

3 configure web.xml

/ mytaglib / WEB-INF/mytaglib.tld

4 introduce in the header of the jsp page that needs to use this tag

5 use (refer to the above steps)

As a result, a simple JSP tag has been developed

Label class description:

The UserInfoTag class we created inherits the TagSupport class, and it implements the Tag interface. The life cycle of the Tag interface is controlled by the container in which it resides, as shown below:

SetPageContext () injects the pageContext of the jsp page in order to access the pageContext property of the jsp page object in the later method

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 the get and set methods that provide the attributes yourself.

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

DoEndTag () is called before the closing tag, returns SKIP_PAGE to skip the output after the entire jsp page, and returns the rest of the EVAL_PAGE execution page

Called at the end of the release () life cycle

Special note: tag buffer pool is enabled by default in versions after tomcat4.1 (websphere and weblogic do not do this), so the release () method (released only when _ jspDestroy ()) is not executed after the tag is executed, that is, no matter how many times the custom tag of the same jsp page is used, there will only be one instance, but not every tag will create a buffer pool, which should be judged by parameters, for example:

In the above example, two label buffer pools are created due to different parameters.

This problem can be solved by setting the configuration file for tomcat:

Add the enablePooling parameter to% tomcat%\ conf\ web.xml and set it to false (custom tags are not cached).

EnablePooling false

Clear the% tomcat%\ conf\ directory

The TagSupport class has implemented and extended some methods for us (for example, in the above methods we can directly use the pageContext object, call the parent tag getParent (), etc.), so in general we just need to override doStartTag (), 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

Goal 2: customize a label similar to the Reapter control in Asp.Net

Effect picture:

Use this custom tag on the jsp page:

UserName Age Email ${item.userName} ${item.age} ${item.email}

Development steps:

To complete this control, we need to implement an iterative interface, IterationTag, and since TagSupport also implements this interface, we inherit this class

1 create a custom label class

Public class Repeater extends TagSupport {private Collection items; private Iterator it; private String var; @ Override public int doStartTag () throws JspException {if (items = = null | | items.size () = 0) return SKIP_BODY; it = items.iterator (); if (it.hasNext ()) {pageContext.setAttribute (var, it.next ()) } return EVAL_BODY_INCLUDE;} @ Override public int doAfterBody () throws JspException {if (it.hasNext ()) {pageContext.setAttribute (var, it.next ()); return EVAL_BODY_AGAIN;} return SKIP_BODY } @ Override public int doEndTag () throws JspException {return EVAL_PAGE;} public void setItems (Collection items) {this.items = items;} public void setVar (String var) {this.var = var;}}

2 create a tag library description file in Web-Inf .tdl (Tag Library Description) since target 1 has already created this file, we only need to add the configuration of this tag

Repeater com.mytags.Repeater jsp items false true var true true

3 configure web.xml (completed in goal 1, no modification required)

4 introduce in the header of the jsp page that needs to use this tag

5 use (refer to the above steps)

Label class description:

We used an iterative interface, and here's how the container handles this interface.

As a supplement to goal 1: in doAfterBody (), if the return value is EVAL_BODY_AGAIN, then this method will be re-executed

Goal 3: use BodyTagSupport

This goal is not shown with actual examples, but mainly shows why and when you need to use the BodyTag interface or BodyTagSupport class?

If we need to be in... . Between the tag body, add some tags or other processing to the head and tail, the general processing method is in the doStartTag and doEndTag methods, but if it is an iterative tag, each segment of the tag body needs to add some tags to the head and tail every time it is looped out. It is very convenient for us to use BodyTagSupport.

This interface returns an extra EVAL_BODY_BUFFERED in the doStartTag () method, which calculates the body and outputs it to the buffer (Note: here the buffer is not directly output to the client, so we need to call the output client manually (this.bodyContent.getEnclosingWriter (). Write (this.bodyContent.getString ());), otherwise the body content will not be displayed.

Label class description:

Description of the BodyTagSupport interface

Goal 4: custom function libraries

1 create a function library class

Public class MyFunctions {public static String formatMyName (String name) {return "your name is" + name;} public static int add (int a, int b) {return astatb;}}

2 configure in the TLD file (tld file referenced in goal 1)

FormatMyName com.taglib.MyFunctions java.lang.String formatMyName (java.lang.String) add com.taglib.MyFunctions java.lang.String add (int, int)

3 call in JSP

${cc:formatMyName ("wangfei")} ${cc:add (12,34)} above is how to get started with JSP custom tag development shared by Xiaobian. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report