我以獲取當前時間的標簽為例,開發一個自定義的標簽bin:currentTime。操作步驟如下:
一、首先要定義一個標簽處理類。
[java]
package org.binbin.tag;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 自定義標簽
* @author binbin([email protected])
* @date 2013-3-28
*/
public class TimeTag extends TagSupport
{
private static final long serialVersionUID = 6918846280074418825L;
@Override
public int doStartTag() throws JspException
{
JspWriter out = this.pageContext.getOut();
//定義時間輸出格式
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//獲取當前時間的指定格式字符串
String str = df.format(new Date());
try
{
//輸出當前時間字符串到頁面
out.print(str);
} catch (IOException e)
{
//如果出現異常,當前程序無法處理,所以重新拋出一個運行時異常
throw new RuntimeException(e);
}
return super.doStartTag();
}
}
package org.binbin.tag;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 自定義標簽
* @author binbin([email protected])
* @date 2013-3-28
*/
public class TimeTag extends TagSupport
{
private static final long serialVersionUID = 6918846280074418825L;
@Override
public int doStartTag() throws JspException
{
JspWriter out = this.pageContext.getOut();
//定義時間輸出格式
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//獲取當前時間的指定格式字符串
String str = df.format(new Date());
try
{
//輸出當前時間字符串到頁面
out.print(str);
} catch (IOException e)
{
//如果出現異常,當前程序無法處理,所以重新拋出一個運行時異常
throw new RuntimeException(e);
}
return super.doStartTag();
}
}
二、然後在WEB-INF目錄下添加一個標簽庫定義文件bin.tld
[html]
<?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">
<tlib-version>1.0</tlib-version>
<short-name>bin</short-name>
<uri>/bin-tags</uri>
<tag>
<name>currentTime</name>
<tag-class>org.binbin.tag.TimeTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
<?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">
<tlib-version>1.0</tlib-version>
<short-name>bin</short-name>
<uri>/bin-tags</uri>
<tag>
<name>currentTime</name>
<tag-class>org.binbin.tag.TimeTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
三、在jsp頁面中使用該標簽
[html]
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 需要先導入相應標簽庫 -->
<%@ taglib uri="/bin-tags" prefix="bin"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP '1.jsp' starting page</title>
</head>
<body>
系統當前時間:<bin:currentTime/>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 需要先導入相應標簽庫 -->
<%@ taglib uri="/bin-tags" prefix="bin"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP '1.jsp' starting page</title>
</head>
<body>
系統當前時間:<bin:currentTime/>
</body>
</html>
四、運行效果