jsp自定义标签

  jsp自定义标签的用途非常重要,其开发业很简单,具体的开发步骤简介为:
1,创建标签处理类,该类应该继承 javax.servlet.jsp.tagext.SimpleTagSupport类
2,创建标签的说明xml文件  tld文件,该文件需要放到WEB-INF下面
3,在页面上引入标签包,使用标签。

实例:创建一个jsp标签,该标签接收5个属性值,查询数据库的值显示在页面上
1,创建标签处理类
package com.supan.tag;
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.Statement;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class QueryTag extends SimpleTagSupport {
	private String driver;
	private String url;
	private String user;
	private String pass;
	private String sql;
	private Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;
	private ResultSetMetaData rsmd = null;
	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 getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	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,pass);
			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='400px'>");
			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 (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(rs != null){
					rs.close();
				}
				if(stmt != null){
					stmt.close();
				}
				if(conn != null){
					conn.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
}


2,创建标签说明的xml文件 my-taglib.xml,注意该文件必须放在WEB-INF下面
<?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>/mytaglib</uri>
    <!-- 定义标签 -->
    <tag>
         <name>query</name>
         <tag-class>com.supan.tag.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>pass</name>
                  <required>true</required>
                  <fragment>true</fragment>
         </attribute>
         <attribute>
                  <name>sql</name>
                  <required>true</required>
                  <fragment>true</fragment>
         </attribute>
    </tag>
</taglib>


3,页面引用标签及使用标签
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="my" uri="/mytaglib" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
      <my:query user="emg" url="jdbc:oracle:thin:@127.0.0.1:1521:ORCL" pass="root" driver="oracle.jdbc.driver.OracleDriver" sql="select * from department"/>
  </body>
</html>

你可能感兴趣的:(jsp自定义标签)