自定义标签

来源:互联网 发布:android程序员校招 编辑:程序博客网 时间:2024/05/29 16:21
Writer out=getJspContext().getOut();

编写标签类继承SimpleTagSupport,定义标签属性的get和set方法

package com.chinaebi.test;import java.io.IOException;import java.io.Writer;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Statement;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class QueryTag extends SimpleTagSupport{   private Connection conn=null;   private String driver;   private String url;   private String user;   private String sql;   private Statement stmt=null;   private ResultSet rs=null;   private ResultSetMetaData rsmd;public Connection getConn() {    return conn;}public void setConn(Connection conn) {    this.conn = conn;}public String getDriver() {    return driver;}public void setDriver(String driver) {    this.driver = driver;}public String getUrl() {    return url;}public void setUrl(String url) {    this.url = url;}public String getUser() {    return user;}public void setUser(String user) {    this.user = user;}public String getSql() {    return sql;}public void setSql(String sql) {    this.sql = sql;}   @Override    public void doTag() throws JspException, IOException {       try {        Class.forName(driver);           conn=DriverManager.getConnection(url, user, null);           stmt=conn.createStatement();           rs=stmt.executeQuery(sql);           rsmd=rs.getMetaData();           Writer out=getJspContext().getOut();           out.write("<table border='1' width='400' cellpadding='0'>");           while(rs.next()){               out.write("<tr>");               for(int i=1;i<=rsmd.getColumnCount();i++){                   //System.out.println(rs.getString(i));                   out.write("<td>"+rs.getString(i)+"<td>");               }               out.write("</tr>");           }    } catch (ClassNotFoundException e) {        e.printStackTrace();    } catch (SQLException e) {        e.printStackTrace();    }finally{        try {            if (rs!=null) {                rs.close();            }            if (stmt!=null) {                stmt.close();            }            if (conn!=null) {                conn.close();            }        } catch (SQLException e) {            e.printStackTrace();        }    }    }}

在WEB_INF下的任意路径建一个标签配置文件.tld文件,具体配置如下

<?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>    <tlib-version>1.0</tlib-version>    <short-name>myTaglib</short-name>    <uri>http://www.chinaebi.org/myTaglib</uri>    <tag>    <description>Outputs Hello, World</description>        <name>helloWorld</name>    <tag-class>com.chinaebi.test.HelloWordTag</tag-class>    <body-content>empty</body-content>    </tag>    <tag>        <name>query</name>        <tag-class>com.chinaebi.test.QueryTag</tag-class>        <body-content>empty</body-content>        <attribute>            <name>driver</name>            <required>true</required>            <fragment>true</fragment>        </attribute>        <attribute>            <name>url</name>            <required>true</required>            <fragment>true</fragment>        </attribute>        <attribute>            <name>user</name>            <required>true</required>            <fragment>true</fragment>        </attribute>        <attribute>            <name>sql</name>            <required>true</required>            <fragment>true</fragment>        </attribute>    </tag>
0 0
原创粉丝点击