JSP custom labels solve time formatting problems for int timestamps in JSP pages
Prior to the project in accordance with the requirements of the need to customize the label, after querying the documents, they also stepped on some pits, I would like to record the steps of the custom label, the following is one of my previous examples of the definition of a time conversion label as an example: GitHub address
1. 在项目中src中建一个com.xiangmuming.tags包,里面建一个类DateTag.java,内容为:
This type of content reference address, at the time of introduction can also refer to this article in Web. XML to configure the path of the label, here I do not configure this.
/** * For page jstl time formatting */public class DateTag extends TagSupport {private static final long Serialversionuid = 6464168398214506236L; private String value; public int doStartTag () throws jspexception {String vv = "" + value; try {simpledateformat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd hh:mm"); String s = Dateformat.format (new Date (Long.valueof (vv+ "000"))); Pagecontext.getout (). write (s); } catch (Exception e) {e.printstacktrace (); } return Super.dostarttag (); The public void SetValue (String value) {this.value = value; } }
Note: In writing a performance-corresponding implementation class, there are several ways to overload the Bodytagsupporrt class: doStartTag (), Setbodycontent (), Doinitbody (), Doafterbody (), Doendtag ( ); Reference addresses
They are executed in the following order: doStartTag () →doinitbody () →setbodycontent () →doafterbody () →doendtag ()
doStartTag () method to return Eval_ Body_include or Skip_body, if return eval_body_include continues execution, if return skip_body then Doinitbody (), Setbodycontent (), Doafterbody ( Three methods are not executed, and the Doendtag () method is executed directly. The Setbodycontent () method is used to set the contents of the label body, which is accomplished in the Doinitbody () method if some initialization work is done before this. When the label body content finishes executing, the Doafterbody () method is called, which returns Eval_body_tag, Skip_body,eval_page, or skip_page. If Eval_body_tag is returned, the label body contents will be set again until the Skip_body is returned, and if Eval_page is returned, the tag body will continue to execute the next part of the JSP page, and if Skip_page is returned, The subsequent content of the JSP page is no longer executed.
2. Build a Mytags.tld file under the Web-inf folder (the file name can be customized), as the label's configuration file, the contents are as follows: <?xml version= "1.0" encoding= "iso-8859-1"? ><! DOCTYPE taglib Public "-//sun Microsystems, Inc.//dtd JSP Tag Library 1.2//en" "Http://java.sun.com/dtd/web-jsptaglibrar Y_1_2.dtd "><taglib> <tlib-version>1.0</tlib-version><!--represents the version number of the tag library--<jsp-version >1.2</jsp-version><!--represents the version of JSP--<short-name>mt</short-name><!--your tag library abbreviation--- <uri>http://xiangmuming.com/mytags</uri><!--your Tag Library Reference URI--<tag> <name>date</n ame><!--The name of the label you define--<tag-class>com.xiangmuming.tags.DateTag</tag-class><!-- Corresponding label handlers: Package name + class name--<attribute> <name>value</name> <required>true< ;/required> <rtexprvalue>true</rtexprvalue> </attribute> <!--<body-conte nt>jsp</body-content>--><!--The format of the contents of the tag body-</tag> </taglib>3. On the JSP page, all the contents of the page are as follows: <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%>//introduce a custom tag library <%@ taglib prefix=" Tmpl "uri=" Http://xinxianggov.com/mytags " %><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
For information on the use of date tags in the Jstl library, refer to
Note: This article is a combination of online information and their own summary of the income, if necessary reprint please specify http://www.cnblogs.com/zhuchenglin/p/8109787.html
JSP custom labels solve time formatting problems for int timestamps in JSP pages