一 新建一个类进行输出时间
这里面 需要传递的数据是date 必须定义getter setter 方法 否则不能赋值
package com.tagtest.test;
import java.io.IOException;
import java.util.Date;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class DateTag extends TagSupport {
Date date;
/**
* @return the date
*/
public Date getDate() {
return date;
}
/**
* @param date
* the date to set
*/
public void setDate(Date date) {
this.date = date;
}
@Override
public int doEndTag() throws JspException {
if (date == null) {
// date = new Date();
}
try {
JspWriter writer = pageContext.getOut();
writer.println(date.toGMTString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return EVAL_PAGE;
}
}
二 在.tld文件里面填写相关信息
<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>A tag who can caculate time</description>
<tlib-version>1.0</tlib-version>
<short-name>util</short-name>
<uri>http://www.sonppengfei.com/util</uri>
<tag>
<description>caculate time</description>
<name>timer</name>
<tag-class>com.tagtest.test.TimerTag</tag-class>
<body-content>JSP</body-content>
</tag>
<tag>
<description>output time</description>
<name>outtime</name>
<tag-class>com.tagtest.test.DateTag</tag-class>
<body-content>empty</body-content>
<attribute>
<description>who can output time</description>
<name>date</name>
<required>false</required> 这个属性是不是必须的
<rtexprvalue>true</rtexprvalue> 运不允许输入jsp表达式
</attribute>
</tag>
</taglib>
三 调用输出
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="song" uri="http://www.sonppengfei.com/util"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
This is my JSP page.
<br>
</body>
<song:timer>
<%
for(int i = 0;i<100000000;i++){
}
%>
</song:timer>
<song:outtime date="<%=new Date(1988,10,27)%>" />
</html>