JSP 自定義標簽實現數據字典的實例
1.關于JSP標簽的好處就不再羅嗦
數據字典就是使用的下拉框,只要定義使用那個字典就會將這個字典可用的內容顯示出來
顯示字典時只要定義那個字典和屬性值就可以顯示出字典的顯示值
2.首先在web.xml中定義自定義標簽加載的引用,兩個屬性分別是引用的URI和加載路徑
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <jsp-config> <taglib> <taglib-uri>/tld/web-html</taglib-uri> <taglib-location> /WEB-INF/tlds/web-html.tld </taglib-location> </taglib> </jsp-config> </web-app>
3.在web-html.tld中定義自己的標簽,數據字典應用的話我們需要一個標簽庫,三個標簽。分別是,select標簽,options標簽,和現實數據字典的標簽,每個標簽都對應不同的實現類
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version><!-- 標簽庫版本 --> <jsp-version>1.2</jsp-version> <!-- 標簽庫要求的JSP規范版本 --> <short-name>html</short-name> <!-- JSP頁面編寫工具可以用來創建助記名的可選名字 --> <tag> <name>select</name> <tag-class>com.SelectTag</tag-class> <body-content>JSP</body-content> <attribute> <name>name</name> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>style</name> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>options</name> <tag-class>com.OptionsTag</tag-class> <body-content>JSP</body-content> <attribute> <name>collection</name> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>selectDisplay</name> <tag-class>com.SelectDisplay</tag-class> <body-content>JSP</body-content> <attribute> <name>collection</name> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>name</name> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>value</name> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
4.實現類
實現類的作用就是在后臺拼接所需HTML標簽內容,然后由JSP進行輸出
實現類最主要的兩個方法,一個遇到這個標簽開始時輸出,一個是結束時輸出
如果需要定義屬性,可以參考實現類定義屬性,并在TLD中定義,在JSP中使用標簽時快捷鍵就可以出來這個屬性
首先是select標簽的代碼:
package com; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspTagException; import javax.servlet.jsp.tagext.BodyTagSupport; /** * TagSupport與BodyTagSupport的區別: * 主要看標簽處理類是否要讀取標簽體的內容和改變標簽體返回的內容,如果不需要就用TagSupport,否則就用BodyTagSupport * 用TagSupport實現的標簽,都可以用BodyTagSupport來實現,因為BodyTagSupport繼承了TagSupport */ @SuppressWarnings("serial") public class SelectTag extends BodyTagSupport { @Override public int doStartTag() throws JspException { try { StringBuffer results = new StringBuffer("<select"); if(name != null){ results.append(" name=/""); results.append(name); results.append("/""); } if(style != null){ results.append(" style=/""); results.append(style); results.append("/""); } results.append(">"); pageContext.getOut().write(results.toString()); } catch (IOException ex) { throw new JspTagException("錯誤"); } return EVAL_BODY_INCLUDE; } @Override public int doEndTag() throws JspException { try { StringBuffer results = new StringBuffer(""); // 因為下拉中包含下拉內容,所以只能在遇到結束標簽時才能寫select結束 results.append("</select>"); pageContext.getOut().write(results.toString()); } catch (IOException ex) { throw new JspTagException("錯誤"); } return EVAL_PAGE; } // 樣式 protected String style; // 名字 protected String name; public String getStyle() { return style; } public void setStyle(String style) { this.style = style; } public String getName() { return name; } public void setName(String name) { this.name = name; } /** doStartTag()方法是遇到標簽開始時會呼叫的方法,其合法的返回值是EVAL_BODY_INCLUDE與SKIP_BODY,前者表示將顯示標簽間的文字,后者表示不顯示標簽間的文字 doEndTag()方法是在遇到標簽結束時呼叫的方法,其合法的返回值是EVAL_PAGE與SKIP_PAGE,前者表示處理完標簽后繼續執行以下的JSP網頁,后者是表示不處理接下來的JSP網頁 doAfterBody(),這個方法是在顯示完標簽間文字之后呼叫的,其返回值有EVAL_BODY_AGAIN與SKIP_BODY,前者會再顯示一次標簽間的文字,后者則繼續執行標簽處理的下一步 EVAL_BODY_INCLUDE:把Body讀入存在的輸出流中,doStartTag()函數可用 EVAL_PAGE:繼續處理頁面,doEndTag()函數可用 SKIP_BODY:忽略對Body的處理,doStartTag()和doAfterBody()函數可用 SKIP_PAGE:忽略對余下頁面的處理,doEndTag()函數可用 EVAL_BODY_BUFFERED:申請緩沖區,由setBodyContent()函數得到的BodyContent對象來處理tag的body,如果類實現了BodyTag,那么doStartTag()可用,否則非法 EVAL_BODY_AGAIN:請求繼續處理body,返回自doAfterBody(),這個返回值在你制作循環tag的時候是很有用的 預定的處理順序是:doStartTag()返回SKIP_BODY,doAfterBodyTag()返回SKIP_BODY,doEndTag()返回EVAL_PAGE 如果繼承了TagSupport之后,如果沒有改寫任何的方法,標簽處理的執行順序是:doStartTag() ->不顯示文字 ->doEndTag()->執行接下來的網頁 如果您改寫了doStartTag(),則必須指定返回值, 如果指定了EVAL_BODY_INCLUDE,則執行順序是:doStartTag()->顯示文字->doAfterBodyTag()->doEndTag()->執行下面的網頁 */ }
關于返回參數,返回具體數字也可以,不用過于糾結
然后是下拉內容實現類
package com; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspTagException; import javax.servlet.jsp.tagext.BodyTagSupport; @SuppressWarnings("serial") public class OptionsTag extends BodyTagSupport { @Override public int doStartTag() throws JspException { return EVAL_BODY_INCLUDE; } @Override public int doEndTag() throws JspException { try { StringBuffer results = new StringBuffer(""); if ("SEX".equals(collection)) { results.append("<option value=/"0/" selected=/"selected/">請選擇</option>"); results.append("<option value=/"1/">男</option>"); results.append("<option value=/"2/">女</option>"); } pageContext.getOut().write(results.toString()); } catch (IOException ex) { throw new JspTagException("錯誤"); } return EVAL_PAGE; } // collection只是傳遞一個標識,具體下拉值內容是從數據庫取還是從請求中得到為不同具體實現 protected String collection; public String getCollection() { return collection; } public void setCollection(String collection) { this.collection = collection; } }
具體你的字典數據從數據庫中如何存儲如何查詢,可以自定義實現
顯示的標簽實現,為了將來可以在頁面取到標簽內容值,我們定義隱藏域來保存屬性值,然后在顯示顯示內容
package com; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspTagException; import javax.servlet.jsp.tagext.BodyTagSupport; @SuppressWarnings("serial") public class SelectDisplay extends BodyTagSupport { @Override public int doStartTag() throws JspException { try { StringBuffer results = new StringBuffer(""); pageContext.getOut().write(results.toString()); } catch (IOException ex) { throw new JspTagException("錯誤"); } return EVAL_BODY_INCLUDE; } @Override public int doEndTag() throws JspException { try { StringBuffer results = new StringBuffer(""); if ("SEX".equals(collection)) { results.append("<span>"); results.append("<input type=/""); results.append("hidden/" name=/""); results.append(getName()); results.append("/""); results.append(" value=/""); results.append(getValue()); results.append("/">"); if ("1".equals(getValue())) { results.append("男"); } else if ("2".equals(getValue())) { results.append("女"); } else { results.append("請選擇"); } results.append("</span>"); } pageContext.getOut().write(results.toString()); } catch (IOException ex) { throw new JspTagException("錯誤"); } return EVAL_PAGE; } // collection只是傳遞一個標識,具體下拉值內容是從數據庫取還是從請求中得到為不同具體實現 protected String collection; // 傳遞的值 protected String value; // 該屬性的名稱 protected String name; public String getCollection() { return collection; } public void setCollection(String collection) { this.collection = collection; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
5.JSP中引用,直接在index.jsp中引用
需要引入相應的標簽內容,引入的方式在JSP頭部引用
標簽的屬性可以設置也可以不設置,標簽的使用和HTML標簽的使用是一樣的,定義屬性即可
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="/tld/web-html" prefix="html"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>JSP 自定義標簽的實現</title> </head> <body> 請選擇: <html:select name="sex" style="width:100px"> <html:options collection="SEX"></html:options> </html:select> 顯示性別: <html:selectDisplay collection="SEX" value="1" name="sex"></html:selectDisplay> </body> </html>
6.后話
訪問項目就可以看到效果,附件是這個項目的源代碼,導入到MyEclipse中可以查看
如果想要自己設計一個大的標簽庫,可以設計一個父類,包含一些主要的屬性,例如name,id,style等屬性。然后在子類中定義自己的特有屬性
這個實現只是學習一下JSP自定義標簽使用的HelloWorld程序,然后包含了字典應用的實際例子,程序簡單,僅供參考
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答