jsp自定義標簽(時間格式化包括Long轉時間)


1、jsp自帶標簽的格式化:

jstl fmt 函數大全:主要針對格式化功能

Tags   
fmt:requestEncoding 
fmt:setLocale 
fmt:timeZone 
fmt:setTimeZone 
fmt:bundle 
fmt:setBundle 
fmt:message 
fmt:param 
fmt:formatNumber 
fmt:parseNumber 
fmt:formatDate 
fmt:parseDate  

 

先在jsp頁面上增加:

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

...

<fmt:formatNumber value="${item.sumAmount}" pattern="##.##" minFractionDigits="2" ></fmt:formatNumber>


 

a. 格式化數字:

<fmt:formatNumber value="${item.sumAmount}" pattern="##.##" minFractionDigits="2" ></fmt:formatNumber>

b. 格式化時間

日期格式(2016年5月25日23點00分23秒)

<fmt:formatDate value="<%=new Date() %>" pattern="yyyy年MM月dd日HH點mm分ss秒" />
<fmt:formatDate value="${date}" pattern="yyyy年MM月dd日HH點mm分ss秒" />
 
        

注意,此時,value必須是date類型的數據,不適合long轉成date.

 

c. long轉成date:

數據庫里存儲的是bigint型的時間,entity實體中存放的是long類型的標簽,現在想輸出到jsp頁面,由於使用的是jstl標簽,而要顯示的是可讀的時間類型,找來找去有個 fmt:formatDate可以轉化,但是只能將String類型的轉成date型,long型則不可以,覺得自定義標簽比較靠譜,也不破環jsp頁面這種標簽結構,參考網上jstl標簽編寫方法,如下:
第一步 寫一個類繼承TagSupport,實現doStartTag() 方法。 

package com.portx.util;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 用於頁面 jstl時間格式化s
 * Created by gmq on 2016/5/24.
 */
public class DateTag extends TagSupport {

    private static final long serialVersionUID = 6464168398214506236L;

    private String value;

    @Override
    public int doStartTag() throws JspException {
        String vv = "" + value;
        try {
            long time = Long.valueOf(vv.trim());
            Calendar c = Calendar.getInstance();
            c.setTimeInMillis(time);
            SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String s = dateformat.format(c.getTime());
            pageContext.getOut().write(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.doStartTag();
    }

    public void setValue(String value) {
        this.value = value;
    }

}

第二步 編寫tld文件,datetag.tld,放在/WEB-INF/tld/目錄下:

<?xml version="1.0" encoding= "UTF-8"?>
<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>1.2</jsp-version>

    <short-name>date</short-name>

    <tag>
        <name>date</name>
        <tag-class>com.portx.util.DateTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>value</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>

第三步  在web.xml中加入引用:

<jsp-config>
        <taglib>
            <taglib-uri>/tags</taglib-uri>
            <taglib-location>/WEB-INF/tld/datetag.tld</taglib-location>
        </taglib>
    </jsp-config>

第四步 在jsp頁面開始使用:

<%@ taglib uri="/tags" prefix="date"%>
....

<date:date value ="${item.createdTime} "/>

此時就可以把long轉換成date類型的string了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM