JSP develops custom labels, custom labels convert strings to the specified time format display

Source: Internet
Author: User
Tags dateformat tld

This example converts the time in string format to the specified time format display.

The first step, define a label handler class, needs to integrate Javax.servlet.jsp.tagext.TagSupport, the code is as follows:

Importjava.io.IOException;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjavax.servlet.jsp.JspException;ImportJavax.servlet.jsp.tagext.TagSupport;/*** Custom time-processing labels * */ Public classJstldatetagextendsTagSupport {/**     *      */    Private Static Final LongSerialversionuid = -8683014812426654300l; PrivateString value;//corresponds to value in JSTL expressionPrivateString parttern;//The time format specified by Partern in the corresponding expression Public intdoStartTag ()throwsjspexception {String VV=string.valueof (value); LongTime =long.valueof (VV); Calendar C=calendar.getinstance ();        C.settimeinmillis (time); SimpleDateFormat DateFormat=NewSimpleDateFormat (Parttern); String s=Dateformat.format (C.gettime ()); Try{pagecontext.getout (). write (s); } Catch(IOException e) {e.printstacktrace (); }        return Super. doStartTag (); }     PublicString GetValue () {returnvalue; }     Public voidSetValue (String value) { This. Value =value; }     PublicString Getparttern () {returnParttern; }     Public voidSetparttern (String parttern) { This. Parttern =Parttern; }}

Step Two, write a file in TLD format. (a file formatted like an XML format) is as follows: Dateformat.tld

<?XML version= "1.0" encoding= "UTF-8"?><taglib>    <tlib-version>1.1</tlib-version>    <jsp-version>1.2</jsp-version>    <Short-name>Date</Short-name>    <Tag>        <name>Stringtodate</name>        <Tag-class>Com.xxx.JSTLDateTag</Tag-class><!--The label you just wrote.        <attribute>            <name>Value</name>            <Required>True</Required>            <Rtexprvalue>True</Rtexprvalue>        </attribute>        <attribute>            <name>Parttern</name>            <Required>True</Required>            <Rtexprvalue>True</Rtexprvalue>        </attribute>    </Tag></taglib>

The third step, add the configuration in Web. XML, find the <jsp-config> node join <taglib> configuration in Web. XML, the code is as follows:

    <Jsp-config>        <taglib>            <Taglib-uri>Http://java.sun.com/jsp/jstl/core</Taglib-uri>            <taglib-location>/web-inf/tld/c.tld</taglib-location>        </taglib>         <!--Custom Jstl time formatting -       <taglib>                   <Taglib-uri>/datetag</Taglib-uri> <!-- take note of the '/datetag ' here.                <taglib-location>/web-inf/tld/dateformat.tld</taglib-location>                   </taglib>       </Jsp-config>

The fourth step, use the custom label in the JSP page, as follows

 <  %@ taglib  uri  = "/datetag"   prefix  = "Fmtdate"  >  <  fmtdate:stringtodate  parttern  = "Yyyy-mm-dd HH:mm:ss"  " Span style= "color: #0000ff;" >= "${hotcontent.createdate}"  ></ fmtdate:stringtodate  >  

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.