jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/MyTag.tld" prefix="cc"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'tagone.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<cc:iterator count="10">Hello world jsp taglib !!!</cc:iterator>
</body>
</html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/MyTag.tld</taglib-uri>
<taglib-location>/WEB-INF/MyTag.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
MyTag.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<tag>
<name>iterator</name>
<tagclass>com.zyujie.util.MyTag</tagclass>
<bodycontent>jsp</bodycontent>
<!-- 定义属性 -->
<attribute>
<name>count</name> <!-- 属性名字 -->
<type>int</type> <!-- 属性类型 -->
<requried>false</requried> <!-- 是否必须 -->
<rtexprvale>false</rtexprvale> <!-- 表示是否可以使用JSP表达式 -->
</attribute>
</tag>
</taglib>
MyTag.class
package com.zyujie.util;
import java.io.IOException;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class MyTag extends BodyTagSupport {
// 执行顺序
//
// doStartTag()->setBodyContent()->doInitBody()->doAfterTag()->doEndTag()
//
// 如果doStartTag()返回的是EVAL_BODY_INCLUDE执行doAfterTag()方法,
//
// 如果它返回SKIP_BODY就执行doEndTag()方法。
//
// setBodyContent()方法用于设置标签体内容,如果在计算BodyContent时需要进行一些初始化工作,
//
// 则在doInitBody()方法中完成。标签体内容执行完后,会调用doAfterBody()方法
//
// 在doAfterTag()方法中返回EVAL_BODY_AGAIN来重复执行doAfterTag()方法
//
// 返回SKIP_BODY值则执行doEndTag()方法。
//
// 在doEndTag()方法中返回EVAL_PAGE值,则执行此标签的后的其它代码,
//
// 返回SKIP_PAGE则不执行此页面的其它代码。
private int count;
private HttpServletRequest reqeust;
private JspWriter out;
public void init() {
reqeust = (HttpServletRequest) pageContext.getRequest();
out = pageContext.getOut();
}
@Override
public int doStartTag() throws JspException {
init();
return this.EVAL_BODY_INCLUDE;
}
// 设置当前标签体
@Override
public void setBodyContent(BodyContent bodyContent) {
this.bodyContent = bodyContent;
System.out.println("setBodyContent...");
}
// 需要初始化bodyContent
@Override
public void doInitBody() throws JspException {
System.out.println("init.....");
}
@Override
public int doAfterBody() throws JspException {
if (count >= 1) {
try {
out.println(count);
out.println("<Br>");
} catch (IOException e) {
e.printStackTrace();
}
count--;
return this.EVAL_BODY_AGAIN;
} else {
return this.SKIP_BODY;
}
}
@Override
public int doEndTag() throws JspException {
java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat("yyyy-MM-dd");
String date = formater.format(new Date());
try {
out.print(date);
} catch (IOException e) {
e.printStackTrace();
}
return this.EVAL_PAGE;
}
// 必须实现setXX()方法
public void setCount(int count) {
this.count = count;
}
}
完成。。。