JSP自定义标签库(javax.servlet.jsp.tagext包)
步骤:1、撰写标签处理类,通常都是extends TagSupport类或者BodyTagSupport类
2、撰写TLD(自定义标签库描述文件.tld)
2、Tag接口
方法: 返回值
doStartTag() int : EVAL_BODY
SKIP_BODY
doEndTag() int : SKIP_PAGE
EVAL_PAGE
release() void
3、IterationTag接口
extends Tag
添加的方法: 返回值
doAfterBody() int :EVAL_BODY_AGAIN
SKIP_BODY
只要运行了自定义标签主体就会运行doAfterBody()方法。
4、TagSupport类
implements IterationTag
生命周期:
1、doStartTag()
2、doAfterBody()//这个方法都不用,因为如果要实现有标签体的标签则应该实现
//BodyTagSupport类,而不是TagSupport类。
3、doEndTag()
4、release()
5、BodyTagSupport类
extends TagSupport implements BodyTag
生命周期:
1、doInitBody()//初始化BodyContent对象
2、doStartTag()//这里由于实现了BodyTag接口所以有三个返回值:
//SKIP_BODY
//EVAL_BODY_INCLUDE
//EVAL_BODY_BUFFERED,用标签
//处理结果建立一个BodyContent对象
3、doAfterBody()
4、doEndTag()
5、release()
6、撰写一个标签处理类的要求:
1、import javax.servlet.jsp.tagext.*;
2、必须为每个属性书写设置器。
3、doStartTag()方法,如果定义的是空标签则必须返回SKIP_BODY。
4、如果不是则应在doAfterBody()中撰写相应的代码。
5、doEndTag()方法通常都让它返回EVAL_PAGE。
6、release()方法中通常撰写回复属性初始值的代码。
撰写中可能使用到的有用的类:JspWriter out =pageContext.getOut();
pageContext是TagSupport类中定义的一个属性,它是PageContext的对象javax.servlet.jsp.PageContext
PageContext类中的常用的方法:
1、findAttribute()//在page,request,session,application作用域中查找
2、getAttribute()//在当前JSP网页中寻找。
3、getServletContext()
4、getSession()
5、getRequest()
7、标签库描述文件(TLD)
TLD是标准的xml文件。
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<description>this si....</description>
<short-name>myT</short-name>
<uri>http://leisure/taglib</uri>
<tag>
<name>FirstTag</name>
<tag-class>leisure.taglib.FirstTag</tag-class>
<body-content>empty</body-content>
<description>this is....</description>
<attribute>
<name>
<type>
<requried>
<rtexprvale>
</attribute>
</tag>
</taglib>
注:body-content可以取empty / jsp / tagdependent三个值//jsp表示可以是任何合法的jsp代码,tagdependent表示
容器不会处理它,只会将它交给标签处理类
<tag>子元素:
<required>可以为true或false
<rtexprvalue>表明属性值是否可以在运行期间产生true或false
8、使用自定义标签库
1、将标签处理类打包成jar文件,放在lib目录下。
2、将tld描述文件放在WEB-INF目录下。
3、在web.xml文件中声明tld。
<taglib>
<taglib-uri>http://leisure/taglib</taglib-uri>
<taglib-location>/WEB-INF/firstTag.tld</taglib-location>
</taglib>
4、在JSP页面中使用。
<%@ taglib uri="http://leisure/taglib" prefix="myTag" %>
使用如下:
<myTag:FirstTag ...../>空标签
2.再建立一个tld文件放置于WEB-INF目录下import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; public class MyTag extends TagSupport{ @Override public int doStartTag() throws JspException { try { this.pageContext.getOut().print("helloworld"); } catch (IOException e) { e.printStackTrace(); } return EVAL_BODY_INCLUDE; } @Override public int doEndTag() throws JspException { try { this.pageContext.getOut().print("welcome"); } catch (IOException e) { e.printStackTrace(); } return EVAL_PAGE; } }
3.引入<%@ taglib uri="/ myTag" prefix="hello"%><?xml version="1.0" encoding="UTF-8"?> <!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.1</tlib-version> <jsp-version>1.2</jsp-version> <short-name>display</short-name> <uri>/myTag</uri> <display-name>Display *: Tag Library</display-name> <description> The display tag library is an open source suite of custom tags that provide high level web presentation patterns which will work in a MVC model, and provide a significant amount of functionality while still being simple and straight-forward to use. The primary tag in the library is the Table tag. </description> <tag> <name>hello</name> <tag-class>MyTag</tag-class> <body-content>JSP</body-content> <display-name>table</display-name> </tag> </taglib>