自定义标签——带属性的标签

带属性的标签

  • 步骤:

    • 创建标签类;
    • 配置标签;
    • 设置标签属性,并使用:
  • 示例代码:

package taglibclass;

import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;


public class QueryTag extends SimpleTagSupport {
	private String driver;
	private String username;
	private String password;
	private String url;
	private String sql;

	private Connection connection = null;
	private PreparedStatement ps = null;
	private ResultSet rs = null;
	private ResultSetMetaData rsmd = null;

	public void doTag() {

		try {
			Class.forName(driver);
			connection = DriverManager.getConnection(url, username, password);
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery();
			rsmd = rs.getMetaData();
			int columnCount = rsmd.getColumnCount();
			Writer out = getJspContext().getOut();
			out.write("<table border='1' width='400px'>");
			while (rs.next()) {
				out.write("<tr>");
				for (int j = 1; j <= columnCount; j++) {
					out.write("<td>");
					out.write(rs.getString(j));
					out.write("</td>");
				}
				out.write("</tr>");
			}
			out.write("</table>");
		} catch (Exception e) {
			e.printStackTrace();
			try {
				throw new JspException("自定义标签错误" + e.getMessage());
			} catch (JspException e1) {
				e1.printStackTrace();
			}
		} finally {
			try{
				if(rs != null) 
					rs.close();
				if(ps != null)
					ps.close();
				if(connection != null)
					connection.close();
			} catch(Exception e) {
				e.printStackTrace();
			}
			
		}
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getSql() {
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}

	public String getDriver() {
		return driver;
	}

	public void setDriver(String driver) {
		this.driver = driver;
	}

}
  • 自定义标签配置:
<tag>
		<name>paramTag</name>
		<tag-class>taglibclass.QueryTag</tag-class>
  <!--设定自定义标签体为空,无此参数会抛出异常-->
		<body-content>empty</body-content>
            <!--设定自定义标签属性-->
		<attribute>
  <!--设定自定义标签属性名-->
			<name>driver</name>
  <!--设定自定义标签属性是否强制要求-->
			<required>true</required>
  <!--设定自定义标签属性是否支持JSP脚本-->
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>url</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>username</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>password</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>sql</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>

  • 标签设置参数,并使用:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="mytaglib/mytag" prefix="mytag" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<mytag:paramTag
	driver="com.mysql.jdbc.Driver"
	url="jdbc:mysql://localhost:3306/userlogin?useUnicode=true&characterEncoding=UTF-8&useSSL=false"
	username="root"
	password="root"
	sql="select * from user"/><br/>
</body>
</html>
原文地址:https://www.cnblogs.com/caoleiCoding/p/9131152.html