一、概述
?????? JSP中有一塊重要的技術(shù):自定義標(biāo)簽(Custom Tag),最近這幾天在學(xué)習(xí)Struts的時(shí)候發(fā)現(xiàn)Struts中使用了很多自定義標(biāo)簽,如html、bean等。所以我就做了個(gè)簡(jiǎn)單的試驗(yàn),學(xué)習(xí)一下這種技術(shù)。
?????? 首先介紹一下這種技術(shù)吧!
1.優(yōu)點(diǎn):
取代了JSP中的Java程序,并且可以重復(fù)使用,方便不熟悉Java編程的網(wǎng)頁(yè)設(shè)計(jì)人員。
2.開(kāi)發(fā)流程:
(1)?????? 編寫(xiě)JSP,在JSP中使用自定義標(biāo)簽。
(2)?????? 在web.xml中指定JSP中使用的標(biāo)簽的.tld(標(biāo)簽庫(kù)描述文件)文件的位置。
(3)?????? .tld文件中指定標(biāo)簽使用的類。
3. 自定義標(biāo)簽的分類:
(1)?????? 簡(jiǎn)單標(biāo)簽:如< mytag:helloworld/>
(2)?????? 帶屬性標(biāo)簽:如<imytag:checkinput dbname = “<myBean.getDBName()>”/>
(3)?????? 帶標(biāo)簽體的標(biāo)簽:
在自定義標(biāo)簽的起始和結(jié)束標(biāo)簽之間的部分為標(biāo)簽體(Body)。Body的內(nèi)容可以是JSP中的標(biāo)準(zhǔn)標(biāo)簽,也可以是HTML、腳本語(yǔ)言或其他的自定義標(biāo)簽。
<mytag:checkinput dbname = “<myBean.getDBName()>”>
????? <mytag:log message=”Table Name”>
<mytag:checkinput />
(4)?????? 可以被Script使用的標(biāo)簽:
定義了id和type屬性的標(biāo)簽可以被標(biāo)簽后面的Scriptlet使用。
<mytag:connection id = “oraDB” type = “DataSource” name = “Oracle”>
<%oraDB.getConnection(); %>
?
4.接口及其他
實(shí)際上,自定義標(biāo)簽的處理類實(shí)現(xiàn)了Tag Handler對(duì)象。JSP技術(shù)在javax.servlet.jsp.tagext中提供了多個(gè)Tag Handler接口,JSP1.2中定義了Tag、BodyTag、IterationTag接口,在JSP2.0中新增了SimpleTag接口。JSP還提供了上述接口的實(shí)現(xiàn)類TagSupport、BodyTagSupport和SimpleTagSupport(SimpleTagSupport只在JSP2.0中才有)。BodyTagSupport實(shí)現(xiàn)了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的縮寫(xiě),意思是:評(píng)價(jià), 估計(jì), 求...的值,在下列的返回值中的意思是執(zhí)行。
返回值
意義
SKIP_BODY
表示不用處理標(biāo)簽體,直接調(diào)用doEndTag()方法。
SKIP_PAGE
忽略標(biāo)簽后面的JSP內(nèi)容。
EVAL_PAGE
處理標(biāo)簽后,繼續(xù)處理JSP后面的內(nèi)容。
EVAL_BODY_BUFFERED
表示需要處理標(biāo)簽體。
EVAL_BODY_INCLUDE
表示需要處理標(biāo)簽體,但繞過(guò)setBodyContent()和doInitBody()方法
EVAL_BODY_AGAIN
對(duì)標(biāo)簽體循環(huán)處理。
?
具體用法可以查看其他參考資料。
Sun的Java教程相關(guān)部分:http://java.sun.com/webservices/docs/1.0/tutorial/doc/JSPTags.html
?
?
二、實(shí)驗(yàn)
1.試驗(yàn)介紹
下面的實(shí)驗(yàn)就是基于上述開(kāi)發(fā)流程開(kāi)發(fā)的。
(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文件中定義實(shí)現(xiàn)標(biāo)簽的處理類:
?? <short-name>mytag</short-name>
?? <tag>
????? <name>helloworld</name>
????? <tag-class>mytag.HelloWorldTag</tag-class>
????? <body-content>empty</body-content>
? </tag>
(4)執(zhí)行處理類mytag.HelloWorldTag的doStartTag和doEndTag方法,然后將結(jié)果輸入到JSP中,和JSP中的內(nèi)容一起輸出。實(shí)際上自定義標(biāo)簽和JSP中的其他的內(nèi)容被WebServer一起編譯成servlet。
?
?
2. 完成后的試驗(yàn)的目錄結(jié)構(gòu)
應(yīng)用myjsp放在Tomcat的webapps下。
myjsp中包含J2EE標(biāo)準(zhǔn)目錄結(jié)構(gòu):WEB-INF和hello.jsp。WEB-INF中包含子目錄classes和lib及web.xml,tld文件可以放在WEB-INF下,也可以放在WEB-INF的子目錄下。
?
?
3.開(kāi)始實(shí)驗(yàn)
3.1.編寫(xiě)JSP
?
< !—hello.jsp的源碼 -- >
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/helloworld" prefix="mytag" %>
<html>
<head>
<title>
jsp1
</title>
</head>
<body bgcolor="#ffffc0">
<h1>
下面顯示的是自定義標(biāo)簽中的內(nèi)容
</h1>
?
<br><br>
<mytag:helloworld></mytag:helloworld>
?
<br>
?
</form>
</body>
</html>
?
3.2.編寫(xiě)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? 編寫(xiě)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>
?
3.4? 編寫(xiě)標(biāo)簽實(shí)現(xiàn)類
?
< !—標(biāo)簽的實(shí)現(xiàn)類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("錯(cuò)誤");
??? }
??? return EVAL_PAGE;
? }
}
?
3.5? 執(zhí)行效果
部署到Tomcat的WebApps目錄下,啟動(dòng)Tomcat,輸入:http://localhost:8080/myjsp/hello.jsp
?
“Hello World”就是我們定義的標(biāo)簽部分的處理類輸出的結(jié)果。
3.6 ?注意:
這個(gè)試驗(yàn)我做了2天,總是報(bào)錯(cuò),弄得很是灰心,開(kāi)始時(shí)以為tld文件或web.xml文件配置不正確,但怎么也找不到原因。
今晚我終于找到原因了,是因?yàn)?class文件一定要放在classes文件夾中,我放到了lib中。
.jar或servlet文件要放在lib目錄中,而.class要放在classes目錄中,如果要放到lib目錄中,必須把mytag中的文件打包成.jar文件,然后把mytag.jar放到lib目錄中。
??
希望你不要犯我犯過(guò)的這個(gè)錯(cuò)誤!^_^
有時(shí)間我會(huì)再寫(xiě)一篇介紹Struts詳細(xì)處理流程的筆記。
這篇筆記整理的的確是JSP1.2的自定義標(biāo)簽,在JSP2.0中xml文件的格式變了,其他的就不太了解了!但我想變化應(yīng)該不會(huì)太大,另外標(biāo)準(zhǔn)是向下兼容的,感興趣的朋友可以看看,這是我的學(xué)習(xí)筆記,可能會(huì)對(duì)別人有些用處,所以貼出來(lái)獻(xiàn)丑了!