JSP custom tag

Source: Internet
Author: User

JSP custom tag. The part between the start and end tags of a custom tag is the Body ).

Tag handler class: defines the behavior of a tag and calls and executes it when the JSP Engine encounters a custom tag.

Tag library Descriptor (TLD) file: Describes the XML document of the tag library and provides the JSP Engine with information about the tag handler for custom tags. Tag attributes: implement a simple tag handler. The tag handler is a Java class called at runtime. It contains the implementation code of custom tags used in JSP files. the tag handler must implement or extend javax. servlet. jsp. classes and interfaces in the tagext package. javax. servlet. jsp. tagext contains interfaces and classes that allow the tag handler class to communicate with the JSP Container.

This technology has recently been used in projects, so I wrote a classic HelloWorld instance myself: In fact, I have already been familiar with custom tags (Struts uses many custom tags, such as html, bean, etc.) advantages of using this technology: replaces the Java program in JSP and can be used repeatedly, so that web designers who are not familiar with Java programming are convenient.

Steps:

Start eclipse and create a web project (nonsense ...)

1. Modify the web. xml file and add custom tags.

Java code

1. <? Xml version = "1.0" encoding = "UTF-8"?>

2. <web-app version = "2.5"

3. xmlns = "http://java.sun.com/xml/ns/javaee"

4. xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"

5. xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee

6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">

7. <jsp-config>

8. <taglib>

9. <taglib-uri>/tld/helloworld </taglib-uri>

10. <taglib-location>/WEB-INF/tlds/helloworld. tld </taglib-location>

11. </taglib>

12. </jsp-config>

13. </web-app>

2. Create a tag library TLD file tlds \ helloworld. tld.

Java code

1. <? Xml version = "1.0" encoding = "UTF-8"?>

2. <! DOCTYPE taglib PUBLIC "-// Sun Microsystems, Inc. // dtd jsp Tag Library 1.2 // EN"

3. "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

4. <taglib>

5. <tlib-version> 1.0 </tlib-version> <! -- Version of the tag library -->

6. <jsp-version> 1.2 </jsp-version> <! -- The JSP standard version required by this tag library -->

7. <short-name> mytag </short-name> <! -- The JSP page writing tool can be used to create an optional name for the nickname -->

8. <tag>

9. <name> helloworld </name> <! -- Unique tag name -->

10. <tag-class> com. yd. mytag. HelloWorldTag </tag-class> <! -- Fully qualified name of the tag HelloWorldTag class -->

11. <body-content> empty </body-content> <! -- Body content type -->

12. </tag>

13. </taglib>

Note: It is important to match the version of the header information of the web. xml and xxx. tld XML files. Otherwise, a page error cannot be found.

3. Create a tag handler class HelloWorldTag (override the doStartTag and doEndTag methods ).

Java code

1. package com. yd. mytag;

2.

3. import java. io. IOException;

4. import javax. servlet. jsp. JspException;

5. import javax. servlet. jsp. JspTagException;

6. import javax. servlet. jsp. tagext. TagSupport;

7 ./**

8. * differences between TagSupport and BodyTagSupport:

9. * It mainly depends on whether the tag processing class needs to read the content of the TAG body and change the content returned by the TAG body. If not, use TagSupport; otherwise, use BodyTagSupport.

10. * All labels implemented using TagSupport can be implemented using BodyTagSupport, because BodyTagSupport inherits TagSupport.

11 .*/

12. public class HelloWorldTag extends TagSupport {

13. private static final long serialVersionUID = 3174234039142131070l;

14. @ Override

15. public int doStartTag () throws JspException {// This method does not need to be returned directly.

16. return EVAL_BODY_INCLUDE;

17 .}

18. @ Override

19. public int doEndTag () throws JspException {// focus on this method

20. try {

21. pageContext. getOut (). write ("Hello World! "); // Tag Return Value

22.} catch (IOException ex ){

23. throw new JspTagException ("error ");

24 .}

25. return EVAL_PAGE;

26 .}

27 .}

Supplement:

The doStartTag () method is a method called when a tag starts. The valid return value is EVAL_BODY_INCLUDE and SKIP_BODY. The former indicates that the text between tags is displayed, the latter indicates that text between tags is not displayed.

The doEndTag () method is the call method when the Tag ends. The valid return values are EVAL_PAGE and SKIP_PAGE. The former indicates that the following JSP page is executed after the tag is processed, the latter indicates that the following JSP page is not processed.

DoAfterBody (). This method is called after the text between labels is displayed. The returned values include EVAL_BODY_AGAIN and SKIP_BODY. The former displays the text between labels again, the latter continues the next step of tag processing.

 

EVAL_BODY_INCLUDE: reads the Body into an existing output stream. The doStartTag () function is available.

EVAL_PAGE: continue to process the page. The doEndTag () function is available.

SKIP_BODY: Ignore the processing of the Body. The doStartTag () and doAfterBody () functions are available.

SKIP_PAGE: Ignore the processing of the remaining pages. The doEndTag () function is available.

EVAL_BODY_BUFFERED: Request the buffer. The BodyContent object obtained by the setBodyContent () function processes the body of the tag. If the class implements BodyTag, doStartTag () is available; otherwise, it is invalid.

EVAL_BODY_AGAIN: The request continues to process the body, and the returned value is from doAfterBody (). This return value is useful when you create a loop tag.

 

The predetermined processing order is: doStartTag () returns SKIP_BODY, doAfterBodyTag () returns SKIP_BODY, doEndTag () returns EVAL_PAGE. If no method is rewritten after TagSupport is inherited, the execution sequence of tag processing is: doStartTag ()-> do not display text-> doEndTag ()-> execute the following web page. If doStartTag () is rewritten, the return value must be specified. If EVAL_BODY_INCLUDE is specified, the execution sequence is: doStartTag ()-> display text-> doAfterBodyTag ()-> doEndTag () -> execute the following webpage.

4. The final test page is hello. jsp.

Java code

1. <% @ page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>

2. <% @ taglib uri = "/tld/helloworld" prefix = "mytag" %> <! -- Declare on the page -->

3. <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">

4.

5.

6. <title> MyJSP </title>

7.

8. <body>

9.

10. <mytag: helloworld> </mytag: helloworld>

11. </body>

12.

 

 

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.