jsp自定標籤

來源:互聯網
上載者:User

標籤:and   extend   star   目錄   des   set   動作   blog   oid   

最近工作中用到了jsp的自訂標籤。在前台使用較少的代碼。即可實現強大的功能,於是通過查詢相關資料做了一個訪問資料的自訂標籤。通過執行個體瞭解自訂標籤的功能。

執行個體來源慕課網

參考資料:http://www.cnblogs.com/shanheyongmu/p/5940945.html

 

建立自訂類,需要繼承TagSupport

package cn.choi.tag;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.TagSupport;@SuppressWarnings("serial")public class DBconnectionTag extends TagSupport {    private String driver;    private String url;    private String sql;    private String username;    private String password;    Connection conn = null;    Statement stmt = null;    ResultSet rs = null;    @Override    public int doEndTag() throws JspException {        try {            Class.forName(this.driver);            conn = DriverManager.getConnection(this.url, this.username,                    this.password);            stmt = conn.createStatement();            rs = stmt.executeQuery(this.sql);            if (rs != null) {                while (rs.next()) {                    pageContext.getOut().print(rs.getString(2) + "<br/>");                }            }            return EVAL_PAGE;        } catch (Exception e) {            e.printStackTrace();            return SKIP_PAGE;        } finally {            try {                if (rs != null) {                    rs.close();                }                if (stmt != null) {                    stmt.close();                }                if (conn != null) {                    rs.close();                }            } catch (Exception e2) {                e2.printStackTrace();            }        }    }    public void setUrl(String url) {        this.url = url;    }    public void setSql(String sql) {        this.sql = sql;    }    public void setUsername(String username) {        this.username = username;    }    public void setPassword(String password) {        this.password = password;    }    public void setDriver(String driver) {        this.driver = driver;    }}

建立tld 檔案,該檔案必須存在WEB-INF目錄或其子目錄。前面兩個Tag為其他是標籤

<?xml version="1.0" encoding="UTF-8" ?><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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"    version="2.0">    <!-- 標籤庫的描述 -->    <description>A tag library exercising SimpleTag handlers</description>    <!-- 自訂標籤的版本為1.0 (必須元素) -->    <tlib-version>1.0</tlib-version>    <!-- 簡短的名稱(元素) -->    <short-name>SimpleTag</short-name>    <!-- 定義此標籤的uri路徑,用於唯一標示的資料庫,便於頁面引用 -->    <uri>/choi-tag</uri>    <tag>        <!--該標籤名稱(必須元素) -->        <name>loginDate</name>        <!-- 全限定名(必須元素) -->        <tag-class>cn.choi.tag.LoginDateTag</tag-class>        <!-- 指明主題類型 -->        <body-content>empty</body-content>    </tag>    <tag>        <description>當頁面request的show值為show時執列標籤體的內容</description>        <name>SkipBodyOrEvalBodyInclude</name>        <tag-class>cn.choi.tag.SkipBodyOrEvalBodyIncludeTag</tag-class>        <!-- 表示標籤體可以包含EL運算式和JSP的動作元素,但不能包含指令碼運算式 -->        <body-content>scriptless</body-content>    </tag>    <tag>        <description>當前的請求只能通過串連或form提交進行</description>        <name>SkipPageOrEvalPage</name>        <tag-class>cn.choi.tag.StartPagorEvalPagTag</tag-class>        <!-- 表示標籤體可以包含EL運算式和JSP的動作元素,但不能包含指令碼運算式 -->        <body-content>empty</body-content>    </tag>    <tag>        <description>串連資料庫</description>        <name>DBconnnectionTag</name>        <tag-class>cn.choi.tag.DBconnectionTag</tag-class>        <body-content>empty</body-content>        <attribute>            <name>driver</name>            <required>true</required>        </attribute>        <attribute>            <name>url</name>            <required>true</required>        </attribute>        <attribute>            <name>sql</name>            <required>true</required>        </attribute>        <attribute>            <name>username</name>            <required>true</required>        </attribute>        <attribute>            <name>password</name>            <required>true</required>        </attribute>    </tag></taglib>

jsp頁面的代碼如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib uri="/choi-tag" prefix="choi"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head></head><body>    <choi:DBconnnectionTag     driver="oracle.jdbc.driver.OracleDriver"    password="admin"     username="bysj"     sql=" select * from users"     url="jdbc:oracle:thin://localhost:1521:orcl"  /></body></html>

 

 

總結:

建立的類需繼承TagSupport類,實現方法。對於傳入的參數需要添加set方法。

tld檔案必須在WEB-INF目錄下,設定參數必須性。

jsp添加taglib引入自訂標籤。

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.