jsp自定義時間轉化tag標籤
阿新 • • 發佈:2018-11-05
1.新建Java型別DateTag
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;
}
}
2.新建datetag.tld檔案放在WEB-INF檔案下
<?xml version="1.0" encoding= "UTF-8"?>
<taglib>
<tlib-version >1.0</tlib-version>
<jsp-version >2.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版本可以在自己的Tomcat找到jsp-api.jar複製出來解壓並檢視
MANIFEST.MF檔案 其中
Specification-Version: 2.2 就是jsp的版本2.2
3 . 在web.xml中加入引用
<jsp-config >
<taglib >
<taglib-uri> /tags</taglib-uri >
<taglib-location> /WEB-INF/datetag.tld</taglib-location >
</taglib >
</jsp-config >
注意檔案路徑是 “/” 不能用 “\”
4 . 在jsp頁面中引用
<%@ taglib uri="/tags" prefix="date"%>
< date:date value ="${trigger.startTime} "/>
轉自:http://blog.csdn.net/tayanxunhua/article/details/20488457