JSP自定义标签库

阅读更多
今天闲来无事,于是想起自己之前一直使用的标签,无论是jstl,struts标签还是springmvc标签,使用起来都觉得很方便,于是自己想尝试定义自己的标签。
下面把今天自己的成果给大家分享下。要自定义标签并使用标签,需要以下几步:
1、定义自己的标签解析类,一般都是继承TagSupport类(servlet-api.jar),Override该类的doStartTag()或者doEndTag()方法;
2、定义.tld文件;
3、在web.xml中声明要使用的标签(也可以不声明);
4、在jsp中使用自定义的标签。
下面通过一个例子来给大家详细的讲解:
1、定义自己的第一个标签解析类:
package com.jrj.tag.example;

import java.io.IOException;

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

@SuppressWarnings("serial")
public class FirstTag extends TagSupport{

	@Override
	public int doEndTag() throws JspException {
		try {
			pageContext.getOut().write("hello world");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
}


2、在web-inf下新建一个tagFile文件夹,然后在文件中新建一个example.tld,内容如下:


	1.0
	1.2
	em
	http://com.jrj.tag/em
	Example
  	em 1.0 library
	
		out
		com.jrj.tag.example.FirstTag
		JSP
		writer out the cotentes
	

3、如果你想在web.xml文件中添加标签的声明,在web.xml中添加如下一段代码:

    	
    		/em
    		/WEB-INF/tagFile/example.tld
    	

note:在今天讲解的这些事例中,我没有在web.xml中添加以上代码,如果添加了那么在后面jsp的引用标签时会有所不同,希望大家注意!
4、在jsp中使用标签,在使用之前需要先引用标签:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib uri="http://com.jrj.tag/em" prefix="em" %>





	



下面我给大家扩展两个其他的例子,一个是关于条件表达式,一个是关于迭代以及标签的嵌套:
1、关于条件表达式,按照上面给大家讲解的步骤:
1)、
package com.jrj.tag.example;

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

@SuppressWarnings("serial")
public class IfTag extends TagSupport{

	private boolean condition;

	@Override
	public int doStartTag() throws JspException {
		
		return condition?EVAL_BODY_INCLUDE:SKIP_BODY;
	}
	public boolean isCondition() {
		return condition;
	}

	public void setCondition(boolean condition) {
		this.condition = condition;
	}
	
	
}

2)、在example.tld中添加如下内容:

		if
		com.jrj.tag.example.IfTag
		JSP
		If Condition
		
			condition
			true
			true
			boolean
			condition
		
		

3)、jsp中使用,jsp修改完后变成如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib uri="http://com.jrj.tag/em" prefix="em" %>






	
	
		if is validate
	
	
	



2、关于迭代及标签的嵌套
1)、标签解析类
package com.jrj.tag.example;

import java.util.Collection;
import java.util.Iterator;

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

@SuppressWarnings("serial")
public class ForEachTag extends BodyTagSupport {

	@SuppressWarnings("rawtypes")
	private Collection items;
	private String var;
	@SuppressWarnings("rawtypes")
	private Iterator it;
	
	@Override
	public int doStartTag() throws JspException {
		if(items==null||items.size()==0)
			return SKIP_BODY;
		it = items.iterator();
		if(it.hasNext()){
			pageContext.setAttribute(var, it.next());
			
		}
		return EVAL_BODY_INCLUDE;
		
	}
	
	@Override
	public int doAfterBody() throws JspException {
		
		if(it.hasNext()){
			pageContext.setAttribute(var, it.next());
			return EVAL_BODY_AGAIN;
			
		}
		return SKIP_BODY;	
	}
	
	@Override
	public int doEndTag() throws JspException {
		
		return EVAL_PAGE;
	}

	@SuppressWarnings("rawtypes")
	public Collection getItems() {
		return items;
	}

	@SuppressWarnings("rawtypes")
	public void setItems(Collection items) {
		this.items = items;
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}
	
}


2)、在example.tld中添加如下内容:

		foreach
		com.jrj.tag.example.ForEachTag
		JSP
		ForEach The Collection
		
			var
			true
			true
		
		
			items
			true
			true
		


3)、jsp中使用,jsp内容如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib uri="http://com.jrj.tag/em" prefix="em" %>
<%@ page import="java.util.*" %>
<%
	String path = request.getContextPath();
	ArrayList values = new ArrayList();
	values.add("zhouming");
	values.add("xiaoshou");
	values.add("minggou");
	pageContext.setAttribute("values", values);
%>





	
	
		if is validate
	
	
		
		${val}
	
	



下面大家可能会对两个方面有疑问:一是java类中每个方法的返回值的含义,二是tld文件中一些元素代表的意思。
1、在我们创建自定义的标签时,在复写父类的方法是,需要注意返回值的含义,不同的返回值代表不同的处理,下面是不同返回值所代表的含义:
EVAL_BODY_INCLUDE:把Body读入存在的输出流中,doStartTag()函数可用
EVAL_PAGE:继续处理页面,doEndTag()函数可用
SKIP_BODY:忽略对Body的处理,doStartTag()和doAfterBody()函数可用
SKIP_PAGE:忽略对余下页面的处理,doEndTag()函数可用
EVAL_BODY_TAG:已经废止,由EVAL_BODY_BUFFERED取代
EVAL_BODY_BUFFERED:申请缓冲区,
由setBodyContent()函数得到的BodyContent对象来处理tag的body,
如果类实现了BodyTag,那么doStartTag()可用,否则非法
2、如果我们需要对body里面的内容进行循环处理,我们自定义的标签解析类需要实现BodyTag接口或者继承BodyTagSupport类
3、在我们自定义的tld文件中,需要注意attribute元素下的rtexprvalue(Run-time Expression value)元素的值,如果该值为true,表示该属性的值可以通过直接指定或者
通过表达式来指定,如果该值为false,则表示该属性的值必须直接指定。
以上就是自定义标签的基本步骤,我今天在做的过程中借鉴了jstl的实现过程以及上网找了一些资料。我觉得这种方式效率很高,如果大家还有什么更加高效的方法,
可以分享出来,大家一起进步!我们下期再见!

你可能感兴趣的:(自定义标签,jsp,java,jstl)