jsp2自訂標籤+屬性

來源:互聯網
上載者:User

標籤:lib   round   utf-8   cut   err   ram   extend   set   support   

前面說過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     @Override52     public void doTag() throws JspException, IOException {53         // TODO Auto-generated method stub54         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 exception80             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 exception95                 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>

 

jsp2自訂標籤+屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.