当前位置: 代码迷 >> Web前端 >> 惯用taglib及用法
  详细解决方案

惯用taglib及用法

热度:222   发布时间:2012-10-10 13:58:11.0
常用taglib及用法

常用taglib及用法

返回Oak>>

?

1. 创建.tld文件,示例如下

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
	version="2.0">
	<description>zzq tag library</description>
	<display-name>zzq taglib</display-name>
	<tlib-version>1.1</tlib-version>
	<short-name>zzq-rj</short-name>
	<uri>http://zzqrj.com/taglibs/mytag</uri>
	<tag>
		<name>dateFormater</name>
		<tag-class>com.neu.edu.utils.mytaglib.DateFormater</tag-class>
		<body-content>empty</body-content>
	    <attribute>
	        <description>
				Date and/or time to be formatted.
	        </description>
	        <name>date</name>
	        <required>true</required>
	        <rtexprvalue>true</rtexprvalue>
	    </attribute>
	</tag>
</taglib>
?

??? 命名该文件为my.tld,存放路径:WEB-INF/tlds/my.tld

?

2. 标签类的实现,如下:

package com.neu.edu.utils.mytaglib;
import java.io.IOException;
import java.util.Date;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class DateFormater extends SimpleTagSupport {
    
    private Date date;

    public void doTag() throws JspException, IOException {
        JspWriter out = getJspContext().getOut();
        out.write(DateFormaterUtils.getFormatDate(date));
        JspFragment f = getJspBody();
        if (f != null) f.invoke(out);
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
    
}

========================
package com.neu.edu.utils.mytaglib;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public final class DateFormaterUtils {

    private static DateFormat BEFORE_YEAR_FORMAT = new SimpleDateFormat("yyyy年M月d日 HH:mm");

    private static DateFormat YEAR_FORMAT = new SimpleDateFormat("M月d日 HH:mm");

    private static DateFormat DAY_FORMAT = new SimpleDateFormat("HH:mm");

    public static String getFormatDate(Date date) {
        if(date!=null){
            Calendar now = GregorianCalendar.getInstance();
            Calendar time = GregorianCalendar.getInstance();
            time.setTime(date);
            if (now.get(Calendar.YEAR) == time.get(Calendar.YEAR)) {
                int day = now.get(Calendar.DAY_OF_YEAR) - time.get(Calendar.DAY_OF_YEAR);
                if (day > 1) {
                    return YEAR_FORMAT.format(date);
                } else if (day == 1) {
                    return "昨天 " + DAY_FORMAT.format(date);
                } else if (day == 0) {
                    int hour = now.get(Calendar.HOUR_OF_DAY) - time.get(Calendar.HOUR_OF_DAY);
                    int minute = hour * 60 + now.get(Calendar.MINUTE) - time.get(Calendar.MINUTE);
                    if (minute > 60) {
                        return "今天 " + DAY_FORMAT.format(date);
                    } else if (minute <= 60 && minute > 5) {
                        return minute + "分钟前 ";
                    } else {
                        return "刚刚更新";
                    }
                }
            } else {
                return BEFORE_YEAR_FORMAT.format(date);
            }
        }
        return null;
    }
}


?

?

3. 在jsp文件中引入并使用

?

??? jsp文件头引入:<%@ taglib uri="http://zzqrj.com/taglibs/mytag " prefix="zzq-rj "%>

??? jsp中使用:<% Date date=new Date();request.setAttribute("date",date); %>
??????????????????? date:<small style="color:#888;"><zzq-rj:dateFormater date="${date}"/>

?

4. 常用taglib

?

??? (1) 文本切割 Go>>

??? (2)


返回Oak>>

  相关解决方案