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

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值