JSP自定义标签的开发及使用
程序员文章站
2022-06-08 23:48:20
...
JSP 2开发自定义标签步骤
1、开发自定义标签处理类;在JSP 2中,处理类需要继承SimpleTagSupport类;
2、建立一个*.tld文件,每个*.tld文件对应一个标签库,每个标签库可以包含多个标签;
3、在JSP文件中使用自定义标签;
使用标签库
在JSP页面使用指定的标签需要确定两点
1、标签库URI:确定使用哪个标签库
2、标签名:确定使用哪个标签;
使用标签库分为以下两个步骤:
1、导入标签库:使用taglib编译指令导入标签库,就是将标签库和指前前缀关联起来;
2、使用标签:在JSP页面中使用自定义标签;语法如下:
<%@ taglib uri="tagliburi" prefix="tagprefix"%>
下面主要介绍带属性的标签、带标签体的标签、以页面片段作为属性的标签及动态属性标签
(1)、带属性的标签
自定义标签处理类QueryTag.java:
package aniyo.tag.demo;
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;
/**
* 带属性的标签
* @author aniyo
* blog:http://aniyo.iteye.com
*
*/
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;
//省略get/set方法。。。
/* (non-Javadoc)
* @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag()
*/
@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='grade' width='300'>");
while(rs.next()){
out.write("<tr>");
out.write("<td>"+rs.getInt(1)+"</td>");
out.write("<td>"+rs.getString(2)+"</td>");
out.write("</tr>");
}
out.write("</table>");
} 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 (Exception e2) {
e2.printStackTrace();
}
}
}
public static void main(String[] args) {
new QueryTag();
}
}
tld文件:querytaglib.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>querytaglib</short-name>
<!-- 定义标签库的uri属性,该属性非常重要 -->
<uri>http://www.crazyit.org/querytaglib</uri>
<tag>
<description>Outputs QueryTag</description>
<name>queryTag</name>
<tag-class>aniyo.tag.demo.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>
jsp页面:QueryTag.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://www.crazyit.org/querytaglib" 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:queryTag
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javaee"
user="root"
pass="123"
sql="select * from news_inf"
/>
</body>
</html>
自定义标签的其他用法见附件