JSP---custom labels

Source: Internet
Author: User
Tags tld

1. First define a class that implements the Label function (Mytagclass.java)

public class Mytagclass extends TagSupport {

@Override//Override doStartTag () method

public int doStartTag () {
HttpServletRequest request;//is a property defined in the TagSupport class, which is an object of Javax.servlet.jsp.PageContext
Request = (HttpServletRequest) pagecontext.getrequest ();
SimpleDateFormat formater = new SimpleDateFormat ("Yyyy-mm-dd");
String strdate = Formater.format (New Date ());
JspWriter out = Pagecontext.getout ();
try {
Out.print (strdate);
} catch (Exception e) {
E.printstacktrace ();
}
The return skip_body;//doStartTag () method returns Skip_body. The reason, of course, is that our simple date tag has no body.

}

}

2. Create a new tag library definition file (MYTAGLIB.TLD)

<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>my</short-name>
<tag>
<name>displaydate</name>//Tag name, custom, map to a class
<tag-class>day01. Mytagclass</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>

3. Adding nodes to the Web. xml file

<jsp-config>
<taglib>
<taglib-uri>myTagLib</taglib-uri>//Tag library file identification, map to tag library file
<taglib-location>/web-inf/mytaglib.tldThe actual address of the </taglib-location>//tag library file
</taglib>
</jsp-config>

4. Add the TAGLIB directive to the JSP file and add a custom label to the body

<%@ taglib uri= "Mytaglib" prefix= "CC"%>//urifor tag library file identification, prefix tag prefix

<body>
<cc:displayDate/>//Find the displaydate tag from the tag library specified by Mytaglib, and then find the class that corresponds to the tag name and execute the doStartTag () method.
</body>

For more information, please refer to: http://blog.csdn.net/zyujie/article/details/8735730

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.