Write your first tag in the JSP

Source: Internet
Author: User
Tags version tld

tag to write a tag involves three steps,

(1) Write JSP

(2) Write tag Java program

(3) Write tag library description file TLD (actually an XML file)

There is no sequential constraint between these three steps, and here is a simple example:

1 Writing hellotag.jsp

<%@page contentType="text/html"%>
<body>
<%@ taglib uri="/WEB-INF/classes/tags/helloTag.tld" prefix="hello" %>
</body>

2 Write Tag

Hellotag.java

package tags; //注意:必须放在一个包中
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class HelloTag extends TagSupport {
public int doStartTag() {
try {
//使用JspWriter获得JSP的输出对象
JspWriter jspWriterOutput = pageContext.getOut();
jspWriterOutput.print("Hello Tag!");
} catch (IOException ioEx) {
System .out.println("IOException in HelloTag " + ioEx);
}
return (SKIP_BODY);
}
}

3 Writing Hellotag.tld

This is the description section of the tag library:

<?xml version= "1.0" encoding= "UTF-8"
<! DOCTYPE taglib
Public "-//sun Microsystems, Inc.//dtd JSP Tag Library 1.2//en"
"http://java.sun.com/dtd/ Web-jsptaglibrary_1_2.dtd "
<taglib>
<tlib-version>1.0</tlib-version>
< Jsp-version>1.2</jsp-version>
<shorttag-name>hellotag</short-name>
<uri>/ Web-inftagshellotag</uri>
<display-name>hellotag</display-name>
<small-icon>< /small-icon>
<large-icon></large-icon>
<description>simple Hello tags tag
</ Description>
<tag>
<name>hellotag</name>
<tag-class >tags. Hellotag</tag-class
<body-content>empty</body-content>
<small-icon></ Small-icon>
<large-icon></large-icon>
<description></description>
< Example></example>
</tag>
</taglib>

4 Note:

The XML file is usually written manually, but the Sun Tutorial recommends using the IDE tool to write custom tags, such as NetBeans

In general, put the TLD file directly into the Web-inf directory.



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.