Customize a simple JSP tag

Source: Internet
Author: User
Tags tld

Web Project, New Java class:

[Java] package leon. webtest. tags;
 
Import java. text. SimpleDateFormat;
Import java. util. Date;
 
Import javax. servlet. jsp. JspException;
Import javax. servlet. jsp. JspWriter;
Import javax. servlet. jsp. tagext. TagSupport;
 
 
Public class DateTime extends TagSupport {
 
Private static final long serialVersionUID = 1L;
 
Public int doStartTag () throws JspException {
Try {
JspWriter out = this. pageContext. getOut ();

SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd hh: mm: ss ");

Out. println ("<span> ");
Out. println (sdf. format (new Date ()));
Out. println ("</span> ");
 
} Catch (Exception e ){
Throw new JspException (e. getMessage ());
}
Return SKIP_BODY;
}
}

Package leon. webtest. tags;

Import java. text. SimpleDateFormat;
Import java. util. Date;

Import javax. servlet. jsp. JspException;
Import javax. servlet. jsp. JspWriter;
Import javax. servlet. jsp. tagext. TagSupport;


Public class DateTime extends TagSupport {

Private static final long serialVersionUID = 1L;

Public int doStartTag () throws JspException {
Try {
JspWriter out = this. pageContext. getOut ();

SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd hh: mm: ss ");

Out. println ("<span> ");
Out. println (sdf. format (new Date ()));
Out. println ("</span> ");

} Catch (Exception e ){
Throw new JspException (e. getMessage ());
}
Return SKIP_BODY;
}
}
New tld file: WEB-INF/tld/webtest-tags.tld

[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<Taglib xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalLocation = "http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
Version = "2.0">
 
<Description> A tag library exercising SimpleTag handlers. </description>
<Tlib-version> 1.0 </tlib-version>
<Short-name> Examples </short-name>
<Uri>/MyTags </uri>
<Description> JSP Self-define Tag Library. </description>
 
<Tag>
<Description> This is an date time tag </description>
<Name> datetime </name>
<Tag-class> leon. webtest. tags. DateTime </tag-class>
<Body-content> empty </body-content>
</Tag>
</Taglib>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Taglib xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalLocation = "http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
Version = "2.0">

<Description> A tag library exercising SimpleTag handlers. </description>
<Tlib-version> 1.0 </tlib-version>
<Short-name> Examples </short-name>
<Uri>/MyTags </uri>
<Description> JSP Self-define Tag Library. </description>

<Tag>
<Description> This is an date time tag </description>
<Name> datetime </name>
<Tag-class> leon. webtest. tags. DateTime </tag-class>
<Body-content> empty </body-content>
</Tag>
</Taglib>
Configuration in web. xml:

[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app id = "WebApp_ID" version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<Display-name> WebTest </display-name>

<Welcome-file-list>
<Welcome-file> index.html </welcome-file>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>

<Jsp-config>
<Taglib>
<Taglib-uri>/MyTags </taglib-uri>
<Taglib-location>/WEB-INF/tld/webtest-tag.tld
</Taglib-location>
</Taglib>
</Jsp-config>
</Web-app>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app id = "WebApp_ID" version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
<Display-name> WebTest </display-name>
 
<Welcome-file-list>
<Welcome-file> index.html </welcome-file>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
 
<Jsp-config>
<Taglib>
<Taglib-uri>/MyTags </taglib-uri>
<Taglib-location>/WEB-INF/tld/webtest-tag.tld
</Taglib-location>
</Taglib>
</Jsp-config>
</Web-app>
Use in JSP:

First, import:

<% @ Taglib uri = "/MyTags" prefix = "leon" %>

 

Use tags:

<Leon: datetime/>

 

Enhanced version: The time to be displayed is dynamic and will jump in seconds like a clock.

 

 

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.