posts - 431,  comments - 344,  trackbacks - 0

          開發和使用自定義標簽程序有三個步驟:
          1.開發標簽實現類
          2.編寫標簽描述,這個描述通常是以.tld結尾的文件
          3.在web.xml中指定標簽庫的引用

          開發實現:

          package com.rain.tag;

          import Java.io.IOException;

          import javax.servlet.jsp.JspException;
          import javax.servlet.jsp.JspTagException;
          import javax.servlet.jsp.PageContext;
          import javax.servlet.jsp.tagext.Tag;

          public class HelloTag implements Tag {

           private PageContext pageContext;
           private Tag parent;
           
           public HelloTag(){
            super();
           }
           public int doEndTag() throws JspException {
            // TODO Auto-generated method stub
            try{
             pageContext.getOut().write("Hello World! 你好");
            }catch(IOException e){
             throw new JspTagException("IO Error:"+e.getMessage());
            }
            return EVAL_PAGE;
           }

           public int doStartTag() throws JspException {
            // TODO Auto-generated method stub
            return SKIP_BODY;  //返回SKIP_BODY,表示不計算標簽體
           }

           public Tag getParent() {
            // TODO Auto-generated method stub
            return this.parent;
           }

           public void release() {
            // TODO Auto-generated method stub
           }

           public void setPageContext(PageContext arg0) {
            // TODO Auto-generated method stub
            this.pageContext=arg0;
           }

           public void setParent(Tag arg0) {
            // TODO Auto-generated method stub
            this.parent=arg0;
           }

          }

          編寫標簽庫描述

          <?xml version="1.0" encoding="UTF-8" ?>
          <taglib xmlns="        xmlns:xsi="        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
                  version="2.0">
                  <description>A tag library exercising SimpleTag handlers.</description>
                  <tlib-version>1.0</tlib-version>
                 
                  <short-name>examples</short-name>
                  <uri>/demotag</uri>
                  <description>JSP應用開發</description>
                 
                  <tag>
                    <description>Outputs Hello,World</description>
                    <name>hello_int</name>
                    <tag-class>com.rain.tag.HelloTag</tag-class>
                    <body-content>empty</body-content>
                  </tag>
          </taglib>

          使用自定義標簽

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "<web-app>
           <taglib>
             <taglib-uri>/demotag</taglib-uri>
             <taglib-location>/WEB-INF/mytag.tld</taglib-location>
           </taglib>
          </web-app>


          <%@ page language="
          Java" contentType="text/html; charset=UTF-8"%>
          <%@ taglib uri="/demotag" prefix="hello" %>
          <html>
          <head>
          <title>Insert title here</title>
          </head>
          <body>
          <p>以下的內容是從Taglib中顯示的:</p>
          <p><i><hello:hello_int/></i></p>
          </body>
          </html>

          本實例是實現Tag接口,但為了在開發中方便簡單,一般直接繼承TagSupport類,只覆蓋doStartTag和doEndTag兩個方法就可以。TagSupport是Tag的子類。

          posted on 2007-01-22 11:53 周銳 閱讀(1405) 評論(0)  編輯  收藏 所屬分類: Jsp
          主站蜘蛛池模板: 罗定市| 千阳县| 南宁市| 正镶白旗| 许昌市| 潼南县| 阜宁县| 阿坝县| 石家庄市| 中西区| 土默特左旗| 凤冈县| 张家港市| 博罗县| 衡阳市| 甘德县| 天峻县| 白河县| 咸阳市| 宜宾县| 北碚区| 宜丰县| 舒兰市| 四子王旗| 伊春市| 和静县| 邻水| 大同市| 东方市| 安阳县| 灵寿县| 思南县| 武清区| 岳池县| 扶余县| 九江县| 什邡市| 宁河县| 富锦市| 桐城市| 益阳市|