jsp標(biāo)簽小結(jié)

          Posted on 2006-07-06 15:05 weibogao 閱讀(646) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): software development
          JSP自定義標(biāo)簽試驗(yàn)原文:兔八哥筆記3:JSP自定義標(biāo)簽試驗(yàn)
          ?

          一、概述

          ?????? JSP中有一塊重要的技術(shù):自定義標(biāo)簽(Custom Tag),最近這幾天在學(xué)習(xí)Struts的時(shí)候發(fā)現(xiàn)Struts中使用了很多自定義標(biāo)簽,如htmlbean等。所以我就做了個(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)簽使用的類(lèi)。

          3. 自定義標(biāo)簽的分類(lèi):

          (1)?????? 簡(jiǎn)單標(biāo)簽:如< mytaghelloworld/>

          (2)?????? 帶屬性標(biāo)簽:如<imytagcheckinput 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)簽。

          <mytagcheckinput dbname = “<myBean.getDBName()>”>

          ????? <mytag:log message=”Table Name”>

          <mytagcheckinput />

          (4)?????? 可以被s cript使用的標(biāo)簽:

          定義了idtype屬性的標(biāo)簽可以被標(biāo)簽后面的s criptlet使用。

          <mytagconnection id = “oraDB” type = “DataSource” name = “Oracle”>

          <%oraDB.getConnection(); %>

          ?

          4.接口及其他

          實(shí)際上,自定義標(biāo)簽的處理類(lèi)實(shí)現(xiàn)了Tag Handler對(duì)象。JSP技術(shù)javax.servlet.jsp.tagext中提供了多個(gè)Tag Handler接口,JSP1.2中定義了TagBodyTagIterationTag接口,在JSP2.0中新增了SimpleTag接口。JSP還提供了上述接口的實(shí)現(xiàn)類(lèi)TagSupportBodyTagSupportSimpleTagSupportSimpleTagSupport只在JSP2.0中才有)。BodyTagSupport實(shí)現(xiàn)了BodyTagTagIterationTag接口。

          ?

          接口及其方法

          Tag接口

          方法

          SimpleTag

          dotage

          Tag

          doStartTag,doEndTag,release

          IterationTag

          doStartTag,doAfterTag,release

          BodyTag

          doStartTag,doEndTag,release,doInitBody,doAfterBody

          ?

          下表引自SunJSP在線(xiàn)教程。

          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

          ?

          下表中的EVALevaluate的縮寫(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)處理。

          ?

          具體用法可以查看其他參考資料。

          SunJava教程相關(guān)部分:java.sun.com/webservices/docs/1.0/tutorial/doc/JSPTags.html">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中指定tagliburi<%@ 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)簽的處理類(lèi):

          ?? <short-name>mytag</short-name>

          ?? <tag>

          ????? <name>helloworld</name>

          ????? <tag-class>mytag.HelloWorldTag</tag-class>

          ????? <body-content>empty</body-content>

          ? </tag>

          (4)執(zhí)行處理類(lèi)mytag.HelloWorldTagdoStartTag和doEndTag方法,然后將結(jié)果輸入到JSP中,和JSP中的內(nèi)容一起輸出。實(shí)際上自定義標(biāo)簽和JSP中的其他的內(nèi)容被WebServer一起編譯成servlet。

          ?

          ?

          2. 完成后的試驗(yàn)的目錄結(jié)構(gòu)

          應(yīng)用myjsp放在Tomcatwebapps下。

          myjsp中包含J2EE標(biāo)準(zhǔn)目錄結(jié)構(gòu):WEB-INFhello.jspWEB-INF中包含子目錄classeslibweb.xml,tld文件可以放在WEB-INF下,也可以放在WEB-INF的子目錄下。

          ?

          ?

          3.開(kāi)始實(shí)驗(yàn)

          31編寫(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>

          ?

          32.編寫(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>


          33? 編寫(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>

          ?以下為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>

          34? 編寫(xiě)標(biāo)簽實(shí)現(xiàn)類(lèi)

          ?

          < !—標(biāo)簽的實(shí)現(xiàn)類(lèi)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;

          ? }

          }

          ?

          35? 執(zhí)行效果

          部署到TomcatWebApps目錄下,啟動(dòng)Tomcat,輸入:jsp/hello.jsp">http://localhost:8080/myjsp/hello.jsp

          ?

          Hello World”就是我們定義的標(biāo)簽部分的處理類(lèi)輸出的結(jié)果。

          posts - 41, comments - 7, trackbacks - 0, articles - 0

          Copyright © weibogao

          主站蜘蛛池模板: 万全县| 庆城县| 三都| 徐水县| 巴彦县| 龙泉市| 云阳县| 饶阳县| 临江市| 沂水县| 湘潭县| 锡林浩特市| 高要市| 吉隆县| 达州市| 江孜县| 洪江市| 黄冈市| 绥芬河市| 交城县| 襄垣县| 深水埗区| 耿马| 荆门市| 化德县| 衡山县| 西宁市| 浠水县| 洛南县| 禹城市| 正安县| 阿拉善左旗| 湟源县| 延边| 潍坊市| 竹溪县| 自治县| 迭部县| 屯留县| 正定县| 修水县|