• 吴裕雄--天生自然JAVA开发JSP-Servlet学习笔记:自定义标签-编写带有属性的标签


    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package le;
    
    import javax.servlet.jsp.tagext.*;
    import javax.servlet.jsp.*;
    import java.io.*;
    import java.sql.*;
    
    /**
     *
     * @author Administrator
     */
    public class QueryTag extends SimpleTagSupport {
        // 定义成员变量来代表标签的属性
    
        private String driver;
        private String url;
        private String user;
        private String pass;
        private String sql;
    
        // driver的setter和getter方法
        public void setDriver(String driver) {
            this.driver = driver;
        }
    
        public String getDriver() {
            return this.driver;
        }
    
        // url的setter和getter方法
        public void setUrl(String url) {
            this.url = url;
        }
    
        public String getUrl() {
            return this.url;
        }
    
        // user的setter和getter方法
        public void setUser(String user) {
            this.user = user;
        }
    
        public String getUser() {
            return this.user;
        }
    
        // pass的setter和getter方法
        public void setPass(String pass) {
            this.pass = pass;
        }
    
        public String getPass() {
            return this.pass;
        }
    
        // sql的setter和getter方法
        public void setSql(String sql) {
            this.sql = sql;
        }
    
        public String getSql() {
            return this.sql;
        }
    
        // conn的setter和getter方法
        public void setConn(Connection conn) {
            this.conn = conn;
        }
    
        public Connection getConn() {
            return this.conn;
        }
    
        // stmt的setter和getter方法
        public void setStmt(Statement stmt) {
            this.stmt = stmt;
        }
    
        public Statement getStmt() {
            return this.stmt;
        }
    
        // rs的setter和getter方法
        public void setRs(ResultSet rs) {
            this.rs = rs;
        }
    
        public ResultSet getRs() {
            return this.rs;
        }
    
        // rsmd的setter和getter方法
        public void setRsmd(ResultSetMetaData rsmd) {
            this.rsmd = rsmd;
        }
    
        public ResultSetMetaData getRsmd() {
            return this.rsmd;
        }
        // 执行数据库访问的对象
        private Connection conn = null;
        private Statement stmt = null;
        private ResultSet rs = null;
        private ResultSetMetaData rsmd = null;
    
        public void doTag() throws JspException,
                IOException {
            try {
                // 注册驱动
                Class.forName(driver);
                // 获取数据库连接
                conn = DriverManager.getConnection(url, user, pass);
                // 创建Statement对象
                stmt = conn.createStatement();
                // 执行查询
                rs = stmt.executeQuery(sql);
                rsmd = rs.getMetaData();
                // 获取列数目
                int columnCount = rsmd.getColumnCount();
                // 获取页面输出流
                Writer out = getJspContext().getOut();
                // 在页面输出表格
                out.write("<table border='1' bgColor='#9999cc' width='400'>");
                // 遍历结果集
                while (rs.next()) {
                    out.write("<tr>");
                    // 逐列输出查询到的数据
                    for (int i = 1; i <= columnCount; i++) {
                        out.write("<td>");
                        out.write(rs.getString(i));
                        out.write("</td>");
                    }
                    out.write("</tr>");
                }
            } catch (ClassNotFoundException cnfe) {
                cnfe.printStackTrace();
                throw new JspException("自定义标签错误" + cnfe.getMessage());
            } catch (SQLException ex) {
                ex.printStackTrace();
                throw new JspException("自定义标签错误" + ex.getMessage());
            } finally {
                // 关闭结果集
                try {
                    if (rs != null) {
                        rs.close();
                    }
                    if (stmt != null) {
                        stmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException sqle) {
                    sqle.printStackTrace();
                }
            }
        }
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
        <tlib-version>1.0</tlib-version>
        <short-name>mytaglib</short-name>
        <uri>/WEB-INF/tlds/mytaglib</uri>
        <!-- A validator verifies that the tags are used correctly at JSP
              translation time. Validator entries look like this: 
           <validator>
               <validator-class>com.mycompany.TagLibValidator</validator-class>
               <init-param>
                  <param-name>parameter</param-name>
                  <param-value>value</param-value>
               </init-param>
           </validator>
        -->
        <!-- A tag library can register Servlet Context event listeners in
             case it needs to react to such events. Listener entries look
             like this: 
          <listener>
              <listener-class>com.mycompany.TagLibListener</listener-class> 
          </listener>
        -->
        
        <!-- 定义第一个标签 -->
        <tag>
            <!-- 定义标签名 -->
            <name>helloWorld</name>
            <!-- 定义标签处理类 -->
            <tag-class>le.HelloWorldTag</tag-class>
            <!-- 定义标签体为空 -->
            <body-content>empty</body-content>
        </tag>
    
        <!-- 定义第二个标签 -->
        <tag>
            <!-- 定义标签名 -->
            <name>query</name>
            <!-- 定义标签处理类 -->
            <tag-class>le.QueryTag</tag-class>
            <!-- 定义标签体为空 -->
            <body-content>empty</body-content>
            <!-- 配置标签属性:driver -->
            <attribute>
                <name>driver</name> 
                <required>true</required>
                <fragment>true</fragment>
            </attribute>
            <!-- 配置标签属性:url -->
            <attribute>
                <name>url</name> 
                <required>true</required>
                <fragment>true</fragment>
            </attribute>
            <!-- 配置标签属性:user -->
            <attribute>
                <name>user</name> 
                <required>true</required>
                <fragment>true</fragment>
            </attribute>
            <!-- 配置标签属性:pass -->
            <attribute>
                <name>pass</name> 
                <required>true</required>
                <fragment>true</fragment>
            </attribute>
            <!-- 配置标签属性:sql -->
            <attribute>
                <name>sql</name> 
                <required>true</required>
                <fragment>true</fragment>
            </attribute>
        </tag>
    
        <!-- 定义第三个标签 -->
        <tag>
            <!-- 定义标签名 -->
            <name>iterator</name>
            <!-- 定义标签处理类 -->
            <tag-class>le.IteratorTag</tag-class>
            <!-- 定义标签体不允许出现JSP脚本 -->
            <body-content>scriptless</body-content>
            <!-- 配置标签属性:collection -->
            <attribute>
                <name>collection</name> 
                <required>true</required>
                <fragment>true</fragment>
            </attribute>
            <!-- 配置标签属性:item -->
            <attribute>
                <name>item</name> 
                <required>true</required>
                <fragment>true</fragment>
            </attribute>
        </tag>
        <tag>
            <!-- 定义标签名 -->
            <name>fragment</name>
            <!-- 定义标签处理类 -->
            <tag-class>le.FragmentTag</tag-class>
            <!-- 指定该标签不支持标签体 -->
            <body-content>empty</body-content>
            <!-- 定义标签属性:fragment -->
            <attribute>
                <name>fragment</name>
                <required>true</required>
                <fragment>true</fragment>
            </attribute>
        </tag>
        <!-- 定义接受动态属性的标签 -->
        <tag>
            <name>dynaAttr</name>
            <tag-class>le.DynaAttributesTag</tag-class>
            <body-content>empty</body-content>
            <!-- 指定支持动态属性 -->
            <dynamic-attributes>true</dynamic-attributes>
        </tag>
        
    </taglib>
    <%-- 
        Document   : queryTag
        Created on : 2020-5-4, 21:40:23
        Author     : Administrator
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <!-- 导入标签库,指定mytag前缀的标签,
            由http://www.crazyit.org/mytaglib的标签库处理 -->
    <%@ taglib uri="/WEB-INF/tlds/mytaglib" prefix="mytag"%>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>自定义标签示范</title>
            <meta name="website" content="http://www.crazyit.org" />
        </head>
        <body bgcolor="#ffffc0">
            <h2>下面显示的是查询标签的结果</h2>
            <!-- 使用标签 ,其中mytag是标签前缀,根据taglib的编译指令,
                    mytag前缀将由http://www.crazyit.org/mytaglib的标签库处理 -->
            <mytag:query
                driver="com.mysql.cj.jdbc.Driver"
                url="jdbc:mysql://localhost:3306/taobao"
                user="root"
                pass="admin"
                sql="select * from person"/><br/>
        </body>
    </html>
  • 相关阅读:
    Qt代码覆盖率code coverage(VS版)
    Qt下Doxygen使用
    QMultiMap使用
    Qt在VS(Visual Studio)中使用
    Qt语言家(Qt Linguist)更新翻译报错Qt5.9MinGW
    Qt Creator插件Todo
    QWidget一生,从创建到销毁事件流
    Qt排序
    QTcpSocketQt使用Tcp通讯实现服务端和客户端
    Qt Creator子目录项目类似VS解决方案
  • 原文地址:https://www.cnblogs.com/tszr/p/12828398.html
Copyright © 2020-2023  润新知