Customize a simple JSP tag

Source: Internet
Author: User
Tags tld

WEB Project, New Java class:

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

<?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:

<?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.