jsp自定义标签

(一) 没有正文的JSP自定义标签实现

(1):定义JSP自定义标签处理类

 

package tags;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

public class DateTags extends TagSupport {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 2169119056578920964L;
	private String pattern;
	
	@Override
	public int doStartTag() throws JspException {

		SimpleDateFormat formatter = new SimpleDateFormat(getPattern());
		String date = formatter.format(new Date());
		JspWriter out = pageContext.getOut();
		try {
			out.println(date);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return Tag.SKIP_BODY;
	}

	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}
}

 

(2) 定义tld文件

<?xml version="1.0" encoding="UTF-8"?>
<taglib>
	<tlibversion>1.0</tlibversion>
	<jspversion>1.1</jspversion>
	<tag>
		<name>date</name>
		<tagclass>tags.DateTags</tagclass>
		<bodycontent>empty</bodycontent>
		<!-- 定义属性 -->
		<attribute>
			<name>pattern</name> 属性名字
			<type>String</type>  属性类型
			<requried>false</requried> 是否必须
			<rtexprvale>false</rtexprvale> 表示是否可以使用JSP表达式
		</attribute>
	</tag>
</taglib>

 

(3) 配置web.xml

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

 

(4) jsp 页面动态引用

<%@ taglib uri="/mytaglib" prefix="cc"%>

<cc:date pattern="yyyy-MM-dd"/>

 

(5)有正文的JSP自定义标签

package tags;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class BodyTag extends BodyTagSupport {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int count;
	
	JspWriter out = null;

	public void init() {
		out = pageContext.getOut();
	}
	
	
	@Override
	public int doStartTag() throws JspException {
		init();
		System.out.println("doStartTag");
		return EVAL_BODY_INCLUDE;
	}

	
	@Override
	public int doAfterBody() throws JspException {
		if (count >= 1) {
			try {
				out.println(count);
				out.println("<BR>");
			} catch (IOException e) {
				// TODO Auto-generated catch block`
				e.printStackTrace();
			}
			count--;
			return EVAL_BODY_AGAIN;
			
		} else {
			return SKIP_BODY;
		}
	}
	
	@Override
	public int doEndTag() throws JspException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = sdf.format(new Date());
		try {
			out.println(date);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}
}

 

<cc:iterator count="5">hello</cc:iterator>

结果
hello4 
hello3 
hello2 
hello1 
hello2012-09-15

 

你可能感兴趣的:(jsp-config,taglib,自定义标签,TagSupport,jsp-jar)