`
wen147258
  • 浏览: 27206 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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,内容如下:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
  PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
  "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
	<tlib-version>1.0</tlib-version>
	<jsp-version>1.2</jsp-version>
	<short-name>em</short-name>
	<uri>http://com.jrj.tag/em</uri>
	<display-name>Example</display-name>
  	<description>em 1.0 library</description>
	<tag>
		<name>out</name>
		<tag-class>com.jrj.tag.example.FirstTag</tag-class>
		<body-content>JSP</body-content>
		<description>writer out the cotentes</description>
	</tag>
</taglib>

3、如果你想在web.xml文件中添加标签的声明,在web.xml中添加如下一段代码:
<jsp-config>
    	<taglib>
    		<taglib-uri>/em</taglib-uri>
    		<taglib-location>/WEB-INF/tagFile/example.tld</taglib-location>
    	</taglib>
</jsp-config>

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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
</head>
<body>
	<em:out></em:out>
</body>
</html>


下面我给大家扩展两个其他的例子,一个是关于条件表达式,一个是关于迭代以及标签的嵌套:
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中添加如下内容:
<tag>
		<name>if</name>
		<tag-class>com.jrj.tag.example.IfTag</tag-class>
		<body-content>JSP</body-content>
		<description>If Condition</description>
		<attribute>
			<name>condition</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>boolean</type>
			<description>condition</description>
		</attribute>
		
</tag>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
</head>
<body>
	<em:out></em:out>
	<em:if condition="${1==1}">
		if is validate
	</em:if>
	
	
</body>
</html>


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中添加如下内容:
<tag>
		<name>foreach</name>
		<tag-class>com.jrj.tag.example.ForEachTag</tag-class>
		<body-content>JSP</body-content>
		<description>ForEach The Collection</description>
		<attribute>
			<name>var</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>items</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
</tag>


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<String> values = new ArrayList<String>();
	values.add("zhouming");
	values.add("xiaoshou");
	values.add("minggou");
	pageContext.setAttribute("values", values);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
</head>
<body>
	<em:out></em:out>
	<em:if condition="${1==1}">
		if is validate
	</em:if>
	<em:foreach items="${pageScope.values}" var="val">
		<em:out></em:out>
		${val}
	</em:foreach>
	
</body>
</html>


下面大家可能会对两个方面有疑问:一是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的实现过程以及上网找了一些资料。我觉得这种方式效率很高,如果大家还有什么更加高效的方法,
可以分享出来,大家一起进步!我们下期再见!
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics