jsp自訂標籤

來源:互聯網
上載者:User

標籤:jsp   標籤   tag   

開發和使用JSP自訂標籤過程:

一、開發標籤實作類別.      HelloTag_Interface.java檔案內容:

import javax.servlet.jsp.tagext.Tag;import javax.servlet.jsp.PageContext;import javax.servlet.jsp.JspException;import java.io.*;import javax.servlet.jsp.JspTagException;/** * 以實現Tag介面的方式來開發標籤程式 */public class HelloTag    implements Tag {  private PageContext pageContext;  private Tag parent;  public HelloTag(){    super();  }  /**   * 設定標籤的頁面上下文   * @param pageContext   */  public void setPageContext(PageContext pageContext) {    this.pageContext=pageContext;  }  /**   * 設定上一級標籤   * @param t   */  public void setParent(Tag parent) {    this.parent=parent;  }  public Tag getParent() {    return this.parent;  }  /**   * 開始標籤時的操作   * @return   * @throws JspException   */  public int doStartTag() throws JspException {    return this.SKIP_BODY;//返回SKIP_BODY,表示不計算標籤體  }  /**   * 結束標籤時的操作   * @return   * @throws JspException   */  public int doEndTag() throws JspException {    try {      pageContext.getOut().write("Hello World!");    }    catch (IOException ex) {      throw new JspTagException("IO Error:"+ex.getMessage());    }    return this.EVAL_PAGE;  }  /**   * Release 用於釋放標籤程式佔用的資源,比如使用了資料庫連接,那麼應該關閉這個串連   */  public void release() {  }}  

二、編寫標籤庫描述.      在WEB-INF/目錄下新建立一個mytag.tld檔案:

<?xml version="1.0" encoding="GBK"?><taglib xmlns="http://java.sun.com/xml/ns/j2ee"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        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>my</short-name>   <uri>/mytag</uri>   <description>學習標籤</description>   <tag>      <description>輸出Hello World</description>      <name>hello</name>      <tag-class>HelloTag</tag-class>      <body-content>empty</body-content>   </tag></taglib>


三、配置自訂標籤到工程中.     在web.xml檔案裡添加:

 

<web-app>...   <taglib>      <taglib-uri>/mytag</taglib-uri>      <taglib-location>/WEB-INF/mytag.tld</taglib-location>   </taglib>...</web-app>

 

四、.在JSP檔案中使用自訂標籤.     編寫mytag.jsp檔案:

 

<%@ taglib uri="/mytag" prefix="hello" %>  <%@ page contentType="text/html;charset=GBK" language="java" %><html>  <head>    <title>first tag</title>    <meta http-equiv="Content-Type" content="text/html;charset=GBK">  </head>  <body>    <p>以下的內容從Taglib中輸出顯示:</p>    <p><my:hello></p>  </body></html>

jsp自訂標籤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.