第一步 寫一個類繼承TagSupport,實現doStartTag() 方法。
package com.vipshop.scheduler.util;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 用於頁面 jstl時間格式化
*
* @Title:
JSTLDateUtils.java
* @Description: TODO(用一句話描述該文件做什麼)
* @author eleven.song(宋濤)
* @date 2014 -3- 4
下午06:28:51
*/
public class DateTag extends TagSupport
{
private static final long serialVersionUID =
6464168398214506236L;
private String value;
@Override
public int doStartTag() throws JspException
{
String vv = ""+value ;
long time
= Long.valueOf(vv);
Calendar c = Calendar. getInstance();
c.setTimeInMillis(time);
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss" );
String s = dateformat.format(c.getTime());
try {
pageContext.getOut().write(s);
} catch (IOException
e) {
e.printStackTrace();
}
return super .doStartTag();
}
public void setValue(String
value) {
this.value =
value;
}
}
|
<?xml version="1.0" encoding= "UTF-8"?>
<taglib>
<tlib-version >1.0</ tlib-version>
<jsp-version >1.2</ jsp-version>
<tag >
<name> date</name >
<tag-class> com.vipshop.scheduler.util.DateTag</tag-class >
<body-content> JSP</body-content >
<attribute>
<name> value</name >
<required> true</required >
<rtexprvalue> true</rtexprvalue >
</attribute>
</tag >
</taglib>
|
<jsp-config >
<taglib >
<taglib-uri> /tags</taglib-uri >
<taglib-location> /WEB-INF/datetag.tld</taglib-location >
</taglib >
</jsp-config >
|
<%@ taglib uri="/tags" prefix="date"%>
< date:date value ="${trigger.startTime} "/>
|