fanzhongzhou

          轉載:jsp標簽小結

          JSP自定義標簽試驗原文:兔八哥筆記3:JSP自定義標簽試驗
           

          一、概述

                 JSP中有一塊重要的技術:自定義標簽(Custom Tag),最近這幾天在學習Struts的時候發現Struts中使用了很多自定義標簽,如htmlbean等。所以我就做了個簡單的試驗,學習一下這種技術。

                 首先介紹一下這種技術吧!

          1.優點:

          取代了JSP中的Java程序,并且可以重復使用,方便不熟悉Java編程的網頁設計人員。

          2開發流程:

          (1)       編寫JSP,在JSP中使用自定義標簽。

          (2)       web.xml中指定JSP中使用的標簽的.tld(標簽庫描述文件)文件的位置。

          (3)       .tld文件中指定標簽使用的類。

          3. 自定義標簽的分類:

          (1)       簡單標簽:如< mytaghelloworld/>

          (2)       帶屬性標簽:如<imytagcheckinput dbname = “<myBean.getDBName()>”/>

          (3)       帶標簽體的標簽:

          在自定義標簽的起始和結束標簽之間的部分為標簽體(Body)。Body的內容可以是JSP中的標準標簽,也可以是HTML、腳本語言或其他的自定義標簽。

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

                <mytag:log message=”Table Name”>

          <mytagcheckinput />

          (4)       可以被s cript使用的標簽:

          定義了idtype屬性的標簽可以被標簽后面的s criptlet使用。

          <mytagconnection 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還提供了上述接口的實現類TagSupportBodyTagSupportSimpleTagSupportSimpleTagSupport只在JSP2.0中才有)。BodyTagSupport實現了BodyTag、TagIterationTag接口。

           

          接口及其方法

          Tag接口

          方法

          SimpleTag

          dotage

          Tag

          doStartTag,doEndTag,release

          IterationTag

          doStartTag,doAfterTag,release

          BodyTag

          doStartTag,doEndTag,release,doInitBody,doAfterBody

           

          下表引自SunJSP在線教程。

          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的縮寫,意思是:評價估計...的值,在下列的返回值中的意思是執行。

          返回值

          意義

          SKIP_BODY

          表示不用處理標簽體,直接調用doEndTag()方法。

          SKIP_PAGE

          忽略標簽后面的JSP內容。

          EVAL_PAGE

          處理標簽后,繼續處理JSP后面的內容。

          EVAL_BODY_BUFFERED

          表示需要處理標簽體。

          EVAL_BODY_INCLUDE

          表示需要處理標簽體,但繞過setBodyContent()doInitBody()方法

          EVAL_BODY_AGAIN

          對標簽體循環處理。

           

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

          SunJava教程相關部分: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中指定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文件中定義實現標簽的處理類:

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

             <tag>

                <name>helloworld</name>

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

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

           </tag>

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

           

           

          2. 完成后的試驗的目錄結構

          應用myjsp放在Tomcatwebapps下。

          myjsp中包含J2EE標準目錄結構:WEB-INFhello.jsp。WEB-INF中包含子目錄classeslibweb.xml,tld文件可以放在WEB-INF下,也可以放在WEB-INF的子目錄下。

           

           

          3.開始實驗

          31編寫JSP

           

          < !—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>

           

          32.編寫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編寫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編寫標簽實現類

           

          < !—標簽的實現類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執行效果
          部署到TomcatWebApps目錄下,啟動Tomcat,輸入:jsp/hello.jsp">http://localhost:8080/myjsp/hello.jspJSP

          posted on 2011-12-23 16:00 jberry 閱讀(209) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2011年12月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 尼勒克县| 都昌县| 宜宾市| 黔西| 德保县| 永丰县| 灵寿县| 武隆县| 客服| 开封市| 鲜城| 武川县| 安阳市| 中方县| 仁怀市| 姚安县| 禄丰县| 德格县| 高雄市| 前郭尔| 疏附县| 色达县| 长沙市| 明水县| 巴林左旗| 贵州省| 兴隆县| 普定县| 钟祥市| 图木舒克市| 克拉玛依市| 肇东市| 平原县| 新巴尔虎右旗| 玉林市| 呼伦贝尔市| 永丰县| 乐安县| 贺州市| 宜宾市| 炉霍县|