欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

开发带属性的标签库实战

程序员文章站 2022-03-11 19:21:11
...

一 开发自定义标签库

package lee;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
import java.sql.*;


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();
            }
        }
    }
}

二 建立TLD文件

<?xml version="1.0" encoding="GBK"?>

<taglib xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">
    <tlib-version>1.0</tlib-version>
    <short-name>mytaglib</short-name>
    <!-- 定义该标签库的URI -->
    <uri>http://www.crazyit.org/mytaglib</uri>


    <!-- 定义第二个标签 -->
    <tag>
        <!-- 定义标签名 -->
        <name>query</name>
        <!-- 定义标签处理类 -->
        <tag-class>lee.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>

</taglib>

三 使用标签

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!-- 导入标签库,指定mytag前缀的标签,
    由http://www.crazyit.org/mytaglib的标签库处理 -->
<%@ taglib uri="http://www.crazyit.org/mytaglib" prefix="mytag"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/javaee"
    user="root"
    pass="32147"
    sql="select * from news_inf"/><br/>
</body>
</html>

四 测试

开发带属性的标签库实战

相关标签: JSP