Java custom tag tutorial and instance code

Source: Internet
Author: User

Test. jsp

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

To change test. jsp to the custom tag method, we use two different methods: simple tag and content tag.

1. Simple tag

Because we need to output two contents (date and file name), we create a parameter for the tag. Code:

DemoTag.java package com.mycompany;import java.util.Date;import javax.servlet.http.*;import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;public class DemoTag extends TagSupport {    public int doStartTag() throws JspException {        try {      HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();      JspWriter out = pageContext.getOut();                  if (parameter.compareToIgnoreCase("filename") == 0)        out.print(request.getServletPath());      else         out.print(new Date());          } catch (java.io.IOException e) {      throw new JspTagException(e.getMessage());    }        return SKIP_BODY;  }    private String parameter = "date";    public void setParameter(String parameter) {    this.parameter = parameter;  }     public String getParameter() {    return parameter;  }}

Next, we will create the Tag file mytaglib. TLD. The tag file is actually a description file in XML format, and the content is also very simple.

Mytaglib. TLD

 

<?xml version="1.0" encoding="ISO-8859-1"?><taglib><tlib-version>1.0</tlib-version><jsp-version>1.2</jsp-version><tag>  <name>demo</name>  <tag-class>com.mycompany.DemoTag</tag-class>  <body-content>empty</body-content>  <attribute>    <name>parameter</name>    <required>false</required>    <rtexprvalue>true</rtexprvalue>  </attribute></tag></taglib>

In this Tag file, we name the created tag named demo and declare the type and parameter (parameter ). Save the file under/WEB-INF.
Of course, we also need to add our custom tags to Web. XML, otherwise they will still be unavailable.

Added in Web. xml:

  <jsp-config>    <taglib>      <taglib-uri>/WEB-INF/MyTagLib</taglib-uri>      <taglib-location>/WEB-INF/MyTagLib.tld</taglib-location>    </taglib>  </jsp-config>

You may have seen similar declarations elsewhere, but there is no external JSP-config, but we use DTD 2.4. If not, eclipse will prompt an error.

So far, our custom tags have been created. Next, we can start to rewrite the JSP file to separate the code.

Test. jsp

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@taglib uri="/WEB-INF/MyTagLib" prefix="mytag"%>

You must be familiar with the above, so I will not say much.

2. Content tag

The creation process is almost the same as above, but there are some differences between program files and configuration content.

Demotag2.java package com. mycompany;

import java.io.IOException;import java.util.Date;import javax.servlet.http.*;import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;public class DemoTag2 extends BodyTagSupport {    public int doStartTag() throws JspTagException {        return EVAL_BODY_BUFFERED;  }    public int doEndTag() throws JspTagException {    String body = this.getBodyContent().getString();    HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();        body = body.replace("$date", new Date().toString());    body = body.replace("$filename", request.getServletPath());        try {      pageContext.getOut().print(body);    }    catch (IOException e) {      throw new JspTagException(e.getMessage());    }        return SKIP_BODY;  }}

Add the new tag demotag2 to the Tag file above.

Mytaglib. TLD

 

<?xml version="1.0" encoding="ISO-8859-1"?><taglib><tlib-version>1.0</tlib-version><jsp-version>1.2</jsp-version><tag>  <name>demo</name>  <tag-class>com.mycompany.DemoTag</tag-class>  <body-content>empty</body-content>  <attribute>    <name>parameter</name>    <required>false</required>    <rtexprvalue>true</rtexprvalue>  </attribute></tag><tag>  <name>demo2</name>  <tag-class>com.mycompany.DemoTag2</tag-class>  <body-content>jsp</body-content></tag></taglib>

The Web. xml file does not need to be modified.

Check the test. jsp effect of both labels.

Test. jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@taglib uri="/WEB-INF/MyTagLib" prefix="mytag"%>

Now, both the label methods are complete.
This article does not describe related technical details. We recommend that you refer to Sun's official documents on JSP custom tags.

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.