jsp2自定义标签+属性
前面说过jsp2自定义标签如果要加入属性需要设置属性的getter和setter方法。代码如下:
1 public class GetParam extends SimpleTagSupport { 2 private String driver; 3 private String url; 4 private String user; 5 private String pwd; 6 private String sql; 7 private Connection conn = null;; 8 private Statement stm = null;; 9 private ResultSet rs = null; 10 11 public void setDriver(String driver) { 12 this.driver = driver; 13 } 14 15 public String getDriver() { 16 return this.driver; 17 } 18 19 public String getUrl() { 20 return url; 21 } 22 23 public void setUrl(String url) { 24 this.url = url; 25 } 26 27 public String getUser() { 28 return user; 29 } 30 31 public void setUser(String user) { 32 this.user = user; 33 } 34 35 public String getPwd() { 36 return pwd; 37 } 38 39 public void setPwd(String pwd) { 40 this.pwd = pwd; 41 } 42 43 public String getSql() { 44 return sql; 45 } 46 47 public void setSql(String sql) { 48 this.sql = sql; 49 } 50 51 @Override 52 public void doTag() throws JspException, IOException { 53 // TODO Auto-generated method stub 54 try { 55 // 注册驱动 56 Class.forName(driver); 57 // 建立连接 58 conn = DriverManager.getConnection(url, user, pwd); 59 stm = conn.createStatement(); 60 rs = stm.executeQuery(sql); 61 // 获取页面输出流 62 Writer out = getJspContext().getOut(); 63 // 设置表格 64 out.write("<table border='1' width='300'>"); 65 // 遍历 66 while (rs.next()) { 67 out.write("<tr>"); 68 for (int i = 1; i < rs.getMetaData().getColumnCount(); i++) { 69 out.write("<td>"); 70 out.write(rs.getString(i)); 71 out.write("</td>"); 72 } 73 out.write("</tr>"); 74 } 75 out.write("</table>"); 76 77 } catch (SQLException | ClassNotFoundException ex) { 78 79 // TODO: handle exception 80 ex.printStackTrace(); 81 throw new JspException("自定义标签错误" + ex.getMessage()); 82 } finally { 83 try { 84 if (rs != null) { 85 rs.close(); 86 } 87 if (stm != null) { 88 stm.close(); 89 } 90 if (conn != null) { 91 conn.close(); 92 } 93 } catch (SQLException e) { 94 // TODO: handle exception 95 e.printStackTrace(); 96 } 97 } 98 } 99 }
接下来配置tld文件,对于有属性的标签,需要在<tag>元素下增加<attribute>子元素。在<attribute>下有三个元素,
第一个是name:设置属性名,第二个是required:该属性是否为必须属性,第三个是fragment:该属性是否支持JSP脚本,表达式等动态内容。
在配置<attribute>下面的name时候,一定要和属性名对应,不然会报错"Unable to find setter method for attribute: XXX",tld文件完整代码如下:
1 <?xml version="1.0" encoding="GBK" ?> 2 <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" 4 version="2.0"> 5 <!-- 描述 --> 6 <description>A tag library exercising SimpleTag handlers.</description> 7 <!-- 版本 --> 8 <tlib-version>1.0</tlib-version> 9 <!-- 短名 --> 10 <short-name>gotparam</short-name> 11 <!-- 指定标签库URI --> 12 <uri>http://tomcat.apache.org/gotparam</uri> 13 <tag> 14 <description>Outputs Hello, World</description> 15 <!-- 标签库名字 --> 16 <name>GetParam</name> 17 <!-- 标签处理类 --> 18 <tag-class>lee.GetParam</tag-class> 19 <!-- 标签体内容 --> 20 <body-content>empty</body-content> 21 <!-- 标签属性:driver --> 22 <attribute> 23 <name>driver</name> 24 <required>true</required> 25 <fragment>true</fragment> 26 </attribute> 27 <!-- 标签属性:url --> 28 <attribute> 29 <name>url</name> 30 <required>true</required> 31 <fragment>true</fragment> 32 </attribute> 33 <!-- 标签属性:user --> 34 <attribute> 35 <name>user</name> 36 <required>true</required> 37 <fragment>true</fragment> 38 </attribute> 39 <!-- 标签属性:pwd --> 40 <attribute> 41 <name>pwd</name> 42 <required>true</required> 43 <fragment>true</fragment> 44 </attribute> 45 <!-- 标签属性:sql --> 46 <attribute> 47 <name>sql</name> 48 <required>true</required> 49 <fragment>true</fragment> 50 </attribute> 51 </tag> 52 </taglib>
最后新建一个jsp文件测试标签,代码如下:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri="http://tomcat.apache.org/gotparam" prefix="gotparam"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <gotparam:GetParam user="root" url="jdbc:mysql://localhost:3306/house" 12 driver="com.mysql.jdbc.Driver" pwd="123456" sql="select * from house" /> 13 </body> 14 </html>