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 >