JSP自定义标签试验原文:兔八哥笔记3:JSP自定义标签试验
 一、概述
       JSP中有一块重要的技术:自定义标签(Custom Tag),最近这几天在学习Struts的时候发现Struts中使用了很多自定义标签,如html、bean等。所以我就做了个简单的试验,学习一下这种技术。
       首先介绍一下这种技术吧!
1.优点:
取代了JSP中的Java程序,并且可以重复使用,方便不熟悉Java编程的网页设计人员。
2.开发流程:
(1)       编写JSP,在JSP中使用自定义标签。
(2)       在web.xml中指定JSP中使用的标签的.tld(标签库描述文件)文件的位置。
(3)       .tld文件中指定标签使用的类。
3. 自定义标签的分类:
(1)       简单标签:如< mytag:helloworld/>
(2)       带属性标签:如<imytag:checkinput dbname = “<myBean.getDBName()>”/>
(3)       带标签体的标签:
在自定义标签的起始和结束标签之间的部分为标签体(Body)。Body的内容可以是JSP中的标准标签,也可以是HTML、脚本语言或其他的自定义标签。
<mytag:checkinput dbname = “<myBean.getDBName()>”>
      <mytag:log message=”Table Name”>
<mytag:checkinput />
(4)       可以被s cript使用的标签:
定义了id和type属性的标签可以被标签后面的s criptlet使用。
<mytag:connection id = “oraDB” type = “DataSource” name = “Oracle”>
<%oraDB.getConnection(); %>
 
4.接口及其他
实际上,自定义标签的处理类实现了Tag Handler对象。JSP技术在javax.servlet.jsp.tagext中提供了多个Tag Handler接口,JSP1.2中定义了Tag、BodyTag、IterationTag接口,在JSP2.0中新增了SimpleTag接口。JSP还提供了上述接口的实现类TagSupport、BodyTagSupport和SimpleTagSupport(SimpleTagSupport只在JSP2.0中才有)。BodyTagSupport实现了BodyTag、Tag和IterationTag接口。
 
接口及其方法
Tag接口  | 方法  | 
SimpleTag  | dotage  | 
Tag  | doStartTag,doEndTag,release  | 
IterationTag  | doStartTag,doAfterTag,release  | 
BodyTag  | doStartTag,doEndTag,release,doInitBody,doAfterBody  | 
 
下表引自Sun的JSP在线教程。
Tag Handler Methods   | 
Tag Handler Type  | Methods  | 
Simple  | doStartTag, doEndTag, release
  | 
Attributes  | doStartTag, doEndTag, set/getAttribute1...N, release
  | 
Body, Evaluation and No Interaction  | doStartTag, doEndTag, release
  | 
Body, Iterative Evaluation  | doStartTag, doAfterBody, doEndTag, release
  | 
Body, Interaction  | doStartTag, doEndTag, release, doInitBody, doAfterBody, release
  | 
 
下表中的EVAL是evaluate的缩写,意思是:评价, 估计, 求...的值,在下列的返回值中的意思是执行。
返回值  | 意义  | 
SKIP_BODY  | 表示不用处理标签体,直接调用doEndTag()方法。  | 
SKIP_PAGE  | 忽略标签后面的JSP内容。  | 
EVAL_PAGE  | 处理标签后,继续处理JSP后面的内容。  | 
EVAL_BODY_BUFFERED  | 表示需要处理标签体。  | 
EVAL_BODY_INCLUDE  | 表示需要处理标签体,但绕过setBodyContent()和doInitBody()方法  | 
EVAL_BODY_AGAIN  | 对标签体循环处理。  | 
 
具体用法可以查看其他参考资料。
Sun的Java教程相关部分:java.sun.com/webservices/docs/1.0/tutorial/doc/JSPTags.html">http://java.sun.com/webservices/docs/1.0/tutorial/doc/JSPTags.html
 
 
二、实验
1.试验介绍
下面的实验就是基于上述开发流程开发的。
(1)在JSP中指定taglib的uri:<%@ taglib uri="/helloworld" prefix="mytag" %>。
(2)在web.xml中配置tag-location:
<taglib>
            <taglib-uri>/helloworld</taglib-uri>
            <taglib-location>/WEB-INF/helloworld.tld</taglib-location>
       </taglib>
       (3)在tag-location中指定的.tld文件中定义实现标签的处理类:
   <short-name>mytag</short-name>
   <tag>
      <name>helloworld</name>
      <tag-class>mytag.HelloWorldTag</tag-class>
      <body-content>empty</body-content>
 </tag>
(4)执行处理类mytag.HelloWorldTag的doStartTag和doEndTag方法,然后将结果输入到JSP中,和JSP中的内容一起输出。实际上自定义标签和JSP中的其他的内容被WebServer一起编译成servlet。
 
 
2. 完成后的试验的目录结构
应用myjsp放在Tomcat的webapps下。
myjsp中包含J2EE标准目录结构:WEB-INF和hello.jsp。WEB-INF中包含子目录classes和lib及web.xml,tld文件可以放在WEB-INF下,也可以放在WEB-INF的子目录下。
 
 
3.开始实验
 
< !—hello.jsp的源码 -- >
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/helloworld" prefix="mytag" %>
<html>
<head>
<title>
jsp1
</title>
</head>
<body bgcolor="#ffffc0">
<h1>
下面显示的是自定义标签中的内容
</h1>
 
<br><br>
<mytag:helloworld></mytag:helloworld>
 
<br>
 
</form>
</body>
</html>
 
3.2.编写web.xml
< !—web.xml的源码 -- >
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSPY v5 rel. 4 U (http://www.xmlspy.com) by Williams (501) -->
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<taglib>
        <taglib-uri>/helloworld</taglib-uri>
        <taglib-location>/WEB-INF/helloworld.tld</taglib-location>
</taglib>
</web-app>
3.3 编写tld文件
 
< !—helloworld.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>mytag</short-name>
   <tag>
       <name>helloworld</name>
       <tag-class>mytag.HelloWorldTag</tag-class>
       <body-content>empty</body-content>
   </tag>
</taglib>
 以下为jsp2.0
<?xml version="1.0" encoding="UTF-8" ?>
<taglib version="2.0">
 <description>test</description>
 <tlib-version>1.0</tlib-version>
 <tag>
  <description>hello world</description>
  <name>title</name>
  <body-content>EMPTY</body-content>
  <tag-class>com.test.tag.helloworld</tag-class>
  <attribute>
   <name>title</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
  </attribute>
 </tag>
</taglib>
3.4 编写标签实现类
 
< !—标签的实现类HelloWorldTag.class的源码 -- >
package mytag;   
 
import java.io.IOException;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
 
public class HelloWorldTag extends TagSupport {    
 public HelloWorldTag() {
 }
 public int doStartTag() throws JspTagException{
    return EVAL_BODY_INCLUDE;
 }
 public int doEndTag() throws JspTagException{
    try {
      pageContext.getOut().write("Hello World");
    }
    catch (IOException ex) {
      throw new JspTagException("错误");
    }
    return EVAL_PAGE;
 }
}
 
 
3.5 执行效果