jsp自定义时间转化tag标签

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

猜你喜欢

转载自blog.csdn.net/qq_30059235/article/details/73912649