国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > JSP1.x 自定义标签

JSP1.x 自定义标签

来源:程序员人生   发布时间:2015-02-02 08:38:00 阅读次数:2607次

Tag接口

任何1个标签都对应着1个java类,该类必须实现Tag接口,JSP遇到1个标签后后,将通过1个tld文件查找该标签的实现类,并运行该类的相干方法

import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.Tag; public class TagTest implements Tag { private Tag parent; private PageContext pageContext; @Override public int doEndTag() throws JspException{ //标签结束时履行 JspWriter out =pageContext.getOut(); try{ out.println("……"); }catch(IOExceptione){ throw new JspException(e); } return EVAL_PAGE; } @Override public int doStartTag() throwsJspException { //标签开始时履行 return SKIP_BODY; } @Override public Tag getParent() { // TODO Auto-generated method stub return null; } @Override public void release() { // TODO Auto-generated method stub } @Override public void setPageContext(PageContextarg0) { // TODO Auto-generated method stub } @Override public void setParent(Tag arg0) { // TODO Auto-generated method stub } }


doStartTag方法可以返回两种参数,如果为SKIP_BODY,表示标签体内的内容不被输出,如果为EVAL_BODY_INCLUDE,则履行标签体内的代码

doEndTag也能够返回两种参数,如果为SKIP_PAGE,表示不履行标签后面的内容。如果为EVAL_PAGE,则履行标签后面的内容

此类中有两个属性,parent和pageContext,parent为该标签的父标签(或是上1层的标签),pageContext为运行该标签的JSP页面,这两个参数都是JSP在运行时通过setter方法注射进去的

 

Tag实现类有了,还需要tld文件(Tag Library Descriptor)

 

<?xml version="1.0" ecoding="UTF⑻"> <taglib xmlns=http://java.sun.com/xml/ns/j2ee xmlns:xsi:="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd version="2.0"> <tlibversion>1.0</tlibversion> <jspversion> 1.1</jspversion> <shortname>tagname</shortname> <uri>http://www.clf.com/tags</uri> <info>A simple tag test</info> <tag> <name>tagname</name> <tagclass>com.chen.tags.TagTest</tagclass> <bodycontent>JSP</ bodycontent> <info>tag information</info> </tag> </taglib>

 

shortname也就是推荐使用的prefix,uri就是援用这个标签库时使用的uri

bodycontent为标签体的限制,有3种取值

empty:不允许有标签体的存在,如果有会抛出异常

JSP:允许有标签体存在,可以为JSP代码

tagdependent:允许有标签体存在,但是标签里的JSP代码不会被履行

 

如果tld文件位于/WEB-INF/下面,Tomcat会自动加载;如果位于其他位置,可以在web.xm中配置

<jsp-config> <taglib> <taglib-uri> http://www.clf.com/tags</taglib-uri> <taglib-location>/WEB-INF/taglib.tld</taglib-location> </taglib> </jsp-config>

TagSupport

多数情况下不需要直接实现Tag接口,使用TagSupport类就能够了,此类是java提供的1个模板类,1般来讲,只需要实现doStartTag方法和doEndTag方法就能够了

 

import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; public class Tagtest extends TagSupport { private int num1; private int num2; @Override public int doEndTag() throws JspException{ try { this.pageContext.getOut().println("两数相加等于:"+(num1+num2)); } catch (Exception e) { } return EVAL_PAGE; } @Override public int doStartTag() throwsJspException { return super.doStartTag(); } public int getNum1() { return num1; } public void setNum1(int num1) { this.num1 = num1; } public int getNum2() { return num2; } public void setNum2(int num2) { this.num2 = num2; } }

 

配置文件

 

<tag> <name>add</name> <tagclass>com.chen.tags.TagTest</tagclass> <bodycontent>JSP</bodycontent> <info>tag information</info> <attribute> <name>num1</name> <required>true</required > <rtexprvalue>true</rtexprvalue > </attribute> <attribute> <name>num2</name> <required>true</required > <rtexprvalue>true</rtexprvalue > </attribute> </tag>

 

name代表属性的名字,required代表是不是是必须的,rtexprvalue代表此属性是不是允许使用EL表达式

以上这个标签这样使用:

<taglib:add num1=”2” num2=”3”/>

则页面会输出2和3 的相加上和

 

BodyTagSupport

BodyTagSupport是Tag接口的子类,专门处理带标签体的标签

public class ToLowerCaseTag extends BodyTagSupport{ public int doEndTag throws JspException{ //获得标签体内的代码 String contend = this.getBodyContent().getString(); try{ this.pageContent.getOut().println(content.toLowerCase()); }catch(Exception e){ } return EVAL_PAGE; } }

只要在setBodyContent()方法以后被调用的方法中,都可使用getBodyContent()方法获得标签体的内容,1般为doAfterBody()或doEndTag()

BodyTagSupport的履行流程以下:

1.当容器创建1个新的标签实例后,通过setPageContext来设置标签的页面上下文.

2.使用setParent方法设置这个标签的上1级标签,如果没有上1级嵌套,设置为null.

3.设置标签的属性,这个属性在标签库描写文件中定义,如果没有定义属性,就不调用此类方法.

4.调用doStartTag方法,这个方法可以返回EVAL_BODY_INCLUDE和SKIP_BODY,当返回EVAL_BODY_INCLUDE时,就计算标签的body,如果返回SKIP_BODY,就不再计算标签的body,如果为EVAL_BODY_BUFFERED,则不会输出,而是将标签体内容通过setBodyContent()方法注射到标签类中,然后就能够使用getBodyContent()获得标签体的内容

5.调用setBodyContent设置当前的BodyContent.

6.调用doInitBody,如果计算BodyContent时需要进行1些初始化,就在这个方法中进行.

7.每次计算完Body后调用doAfterBody,如果返回EVAL_BODY_AGAIN,表示继续计算1次Body,直到返回SKIP_BODY才继续往下履行.

8.调用doEndTag方法,这个方法可以返回EVAL_PAGE或SKIP_PAGE,当返回EVAL_PAGE时,容器将在标签结束时继续计算JSP页面其他的部份;如果返回SKIP_PAGE,容器将在标签结束时停止计算JSP页面其他的部份.

9.调用release()方法释放标签程序占用的任何资源。

 

 

public class LoopTag extends BodyTagSupport{ private int times; public int doStartTag() throwsJspException{ times = 5; return super.doStartTag(); } public int doAfterBody() throwsJspException{ if(times-- >0){ 只要times大于零继续循环,同时times自减 try{ this.getPreviousOut().println(this.getBodyContent().getString()); }catch(Exception e){ } return EVAL_BODY_AGAIN; //继续履行 }else{ return SKIP_BODY; } } }

以上定义了1个循环标签

<taglib:loop>loop.</taglib:loop>

会得到以下输出:

loop.

loop. loop.

loop. loop. loop.

loop. loop. loop.loop.

loop. loop. loop.loop. loop.

 

实际上,doAfterBody方法内的输出是写到bodyContent缓存中的

BodyTagSupport中,里面的标签可以通过getparent()方法取得上层的标签

 

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生