最近工作中用到了jsp的自定义标签。在前台使用较少的代码。即可实现强大的功能,于是通过查询相关资料做了一个访问数据的自定义标签。通过实例了解自定义标签的功能。
实例来源慕课网
参考资料:http://www.cnblogs.com/shanheyongmu/p/5940945.html
创建自定义类,需要继承TagSupport
package cn.choi.tag; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; @SuppressWarnings("serial") public class DBconnectionTag extends TagSupport { private String driver; private String url; private String sql; private String username; private String password; Connection conn = null; Statement stmt = null; ResultSet rs = null; @Override public int doEndTag() throws JspException { try { Class.forName(this.driver); conn = DriverManager.getConnection(this.url, this.username, this.password); stmt = conn.createStatement(); rs = stmt.executeQuery(this.sql); if (rs != null) { while (rs.next()) { pageContext.getOut().print(rs.getString(2) + "<br/>"); } } return EVAL_PAGE; } catch (Exception e) { e.printStackTrace(); return SKIP_PAGE; } finally { try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { rs.close(); } } catch (Exception e2) { e2.printStackTrace(); } } } public void setUrl(String url) { this.url = url; } public void setSql(String sql) { this.sql = sql; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public void setDriver(String driver) { this.driver = driver; } }
创建tld 文件,该文件必须存在WEB-INF目录或其子目录。前面两个Tag为其他是标签
<?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> <!-- 自定义标签的版本为1.0 (必须元素) --> <tlib-version>1.0</tlib-version> <!-- 简短的名称(元素) --> <short-name>SimpleTag</short-name> <!-- 定义此标签的uri路径,用于唯一标示的数据库,便于页面引用 --> <uri>/choi-tag</uri> <tag> <!--该标签名称(必须元素) --> <name>loginDate</name> <!-- 全限定名(必须元素) --> <tag-class>cn.choi.tag.LoginDateTag</tag-class> <!-- 指明主题类型 --> <body-content>empty</body-content> </tag> <tag> <description>当页面request的show值为show时执行标签体的内容</description> <name>SkipBodyOrEvalBodyInclude</name> <tag-class>cn.choi.tag.SkipBodyOrEvalBodyIncludeTag</tag-class> <!-- 表示标签体可以包含EL表达式和JSP的动作元素,但不能包含脚本表达式 --> <body-content>scriptless</body-content> </tag> <tag> <description>当前的请求只能通过连接或form提交进行</description> <name>SkipPageOrEvalPage</name> <tag-class>cn.choi.tag.StartPagorEvalPagTag</tag-class> <!-- 表示标签体可以包含EL表达式和JSP的动作元素,但不能包含脚本表达式 --> <body-content>empty</body-content> </tag> <tag> <description>连接数据库</description> <name>DBconnnectionTag</name> <tag-class>cn.choi.tag.DBconnectionTag</tag-class> <body-content>empty</body-content> <attribute> <name>driver</name> <required>true</required> </attribute> <attribute> <name>url</name> <required>true</required> </attribute> <attribute> <name>sql</name> <required>true</required> </attribute> <attribute> <name>username</name> <required>true</required> </attribute> <attribute> <name>password</name> <required>true</required> </attribute> </tag> </taglib>
jsp页面的代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/choi-tag" prefix="choi"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <choi:DBconnnectionTag driver="oracle.jdbc.driver.OracleDriver" password="admin" username="bysj" sql=" select * from users" url="jdbc:oracle:thin://localhost:1521:orcl" /> </body> </html>
总结:
创建的类需继承TagSupport类,实现方法。对于传入的参数需要添加set方法。
tld文件必须在WEB-INF目录下,设置参数必须性。
jsp添加taglib引入自定义标签。
时间: 2024-10-01 02:39:45