歡迎您光臨本站 註冊首頁

JSP自定義標籤實現數據字典

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

數據字典就是使用的下拉框,只要定義使用那個字典就會將這個字典可用的內容顯示出來.本文將講解怎麼自定義標籤實現數據字典.

1、關於JSP標籤的好處就不再羅嗦

數據字典就是使用的下拉框,只要定義使用那個字典就會將這個字典可用的內容顯示出來

顯示字典時只要定義那個字典和屬性值就可以顯示出字典的顯示值

2、首先在web.xml中定義自定義標籤載入的引用,兩個屬性分別是引用的URI和載入路徑

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6. <welcome-file-list>
  7. <welcome-file>index.jsp</welcome-file>
  8. </welcome-file-list>
  9. <jsp-config>
  10. <taglib>
  11. <taglib-uri>/tld/web-html</taglib-uri>
  12. <taglib-location

    >
  13. /WEB-INF/tlds/web-html.tld
  14. </taglib-location>
  15. </taglib>
  16. </jsp-config>
  17. </web-app>

3、在web-html.tld中定義自己的標籤,數據字典應用的話我們需要一個標籤庫,三個標籤.分別是,select標籤,options標籤,和現實數據字典的標籤,每個標籤都對應不同的實現類

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
  3. "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
  4. <taglib>
  5. <tlib-version>1.0</tlib-version><!-- 標籤庫版本 -->
  6. <jsp-version>1.2</jsp-version> <!-- 標籤庫要求的JSP規範版本 -->
  7. <short-name>html</short-name> <!-- JSP頁面編寫工具可以用來創建助記名的可選名字 -->
  8. <tag>
  9. <name>select</name>

  10. <tag-class>com.SelectTag</tag-class>
  11. <body-content>JSP</body-content>
  12. <attribute>
  13. <name>name</name>
  14. <rtexprvalue>true</rtexprvalue>
  15. </attribute>
  16. <attribute>
  17. <name>style</name>
  18. <rtexprvalue>true</rtexprvalue>
  19. </attribute>
  20. </tag>
  21. <tag>
  22. <name>options</name>
  23. <tag-class>com.OptionsTag</tag-class>
  24. <body-content>JSP</body-content

    >
  25. <attribute>
  26. <name>collection</name>
  27. <rtexprvalue>true</rtexprvalue>
  28. </attribute>
  29. </tag>
  30. <tag>
  31. <name>selectDisplay</name>
  32. <tag-class>com.SelectDisplay</tag-class>
  33. <body-content>JSP</body-content>
  34. <attribute>
  35. <name>collection</name>
  36. <rtexprvalue>true</rtexprvalue>
  37. </attribute>
  38. <attribute>
  39. <name>name</name>

  40. <rtexprvalue>true</rtexprvalue>
  41. </attribute>
  42. <attribute>
  43. <name>value</name>
  44. <rtexprvalue>true</rtexprvalue>
  45. </attribute>
  46. </tag>
  47. </taglib>

4、實現類

實現類的作用就是在後台拼接所需HTML標籤內容,然後由JSP進行輸出

實現類最主要的兩個方法,一個遇到這個標籤開始時輸出,一個是結束時輸出

如果需要定義屬性,可以參考實現類定義屬性,並在TLD中定義,在JSP中使用標籤時快捷鍵就可以出來這個屬性

首先是select標籤的代碼:

  1. package com;
  2. import java.io.IOException;
  3. import javax.servlet.jsp.JspException;
  4. import javax.servlet.jsp.JspTagException;
  5. import javax.servlet.jsp.tagext.BodyTagSupport;
  6. /**
  7. * TagSupport與BodyTagSupport的區別:
  8. * 主要看標籤處理類是否要讀取標籤體的內容和改變標籤體返回的內容,如果不需要就用TagSupport,否則就用BodyTagSupport
  9. * 用TagSupport實現的標籤,都可以用BodyTagSupport來實現,BodyTagSupport繼承了TagSupport

  10. */
  11. @SuppressWarnings("serial")
  12. public class SelectTag extends BodyTagSupport {
  13. @Override
  14. public int doStartTag() throws JspException {
  15. try {
  16. StringBuffer results = new StringBuffer("<select");
  17. if(name != null){
  18. results.append(" name=\"");
  19. results.append(name);
  20. results.append("\"");
  21. }
  22. if(style != null){
  23. results.append(" style=\"");
  24. results.append(style);
  25. results.append("\"");
  26. }
  27. results.append(">");
  28. pageContext.getOut().write(results.toString());
  29. } catch (IOException ex) {
  30. throw new JspTagException("錯誤");
  31. }
  32. return EVAL_BODY_INCLUDE;
  33. }
  34. @Override
  35. public int doEndTag() throws JspException {
  36. try {
  37. StringBuffer results = new

    StringBuffer("");
  38. // 下拉中包含下拉內容,只能在遇到結束標籤時才能寫select結束
  39. results.append("</select>");
  40. pageContext.getOut().write(results.toString());
  41. } catch (IOException ex) {
  42. throw new JspTagException("錯誤");
  43. }
  44. return EVAL_PAGE;
  45. }
  46. // 樣式
  47. protected String style;
  48. // 名字
  49. protected String name;
  50. public String getStyle() {
  51. return style;
  52. }
  53. public void setStyle(String style) {
  54. this.style = style;
  55. }
  56. public String getName() {
  57. return name;
  58. }
  59. public void setName(String name) {
  60. this.name = name;
  61. }
  62. /**
  63. doStartTag()方法是遇到標籤開始時會呼叫的方法,其合法的返回值是EVAL_BODY_INCLUDE與SKIP_BODY,前者表示將顯示標籤間的文字,後者表示不顯示標籤間的文字
  64. doEndTag()方法是在遇到標籤結束時呼叫的方法,其合法的返回值是EVAL_PAGE與SKIP_PAGE,前者表示處理完標籤後繼續執行以下的JSP網頁,後者是表示不處理接下來的JSP網頁
  65. doAfterBody(),這個方法是在顯示完標籤間文字之後呼叫的,其返回值有EVAL_BODY_AGAIN與SKIP_BODY,前者會再顯示一次標籤間的文字,後者則繼續執行標籤處理的下一步

  66. EVAL_BODY_INCLUDE:把Body讀入存在的輸出流中,doStartTag()函數可用
  67. EVAL_PAGE:繼續處理頁面,doEndTag()函數可用
  68. SKIP_BODY:忽略對Body的處理,doStartTag()和doAfterBody()函數可用
  69. SKIP_PAGE:忽略對餘下頁面的處理,doEndTag()函數可用
  70. EVAL_BODY_BUFFERED:申請緩衝區,由setBodyContent()函數得到的BodyContent對象來處理tag的body,如果類實現了BodyTag,那麼doStartTag()可用,否則非法
  71. EVAL_BODY_AGAIN:請求繼續處理body,返回自doAfterBody(),這個返回值在你製作循環tag的時候是很有用的
  72. 預定的處理順序是:doStartTag()返回SKIP_BODY,doAfterBodyTag()返回SKIP_BODY,doEndTag()返回EVAL_PAGE
  73. 如果繼承了TagSupport之後,如果沒有改寫任何的方法,標籤處理的執行順序是:doStartTag() ->不顯示文字 ->doEndTag()->執行接下來的網頁
  74. 如果您改寫了doStartTag(),則必須指定返回值,
  75. 如果指定了EVAL_BODY_INCLUDE,則執行順序是:doStartTag()->顯示文字->doAfterBodyTag()->doEndTag()->執行下面的網頁
  76. */
  77. }

關於返回參數,返回具體數字也可以,不用過於糾結

然後是下拉內容實現類

  1. package com;
  2. import java.io.IOException;
  3. import javax.servlet.jsp.JspException;
  4. import javax.servlet.jsp.JspTagException;
  5. import javax.servlet.jsp.tagext.BodyTagSupport;
  6. @SuppressWarnings("serial")
  7. public class OptionsTag extends BodyTagSupport {
  8. @Override
  9. public int doStartTag() throws

    JspException {
  10. return EVAL_BODY_INCLUDE;
  11. }
  12. @Override
  13. public int doEndTag() throws JspException {
  14. try {
  15. StringBuffer results = new StringBuffer("");
  16. if ("SEX".equals(collection)) {
  17. results.append("<option value=\"0\" selected=\"selected\">請選擇</option>");
  18. results.append("<option value=\"1\">男</option>");
  19. results.append("<option value=\"2\">女</option>");
  20. }
  21. pageContext.getOut().write(results.toString());
  22. } catch (IOException ex) {
  23. throw new JspTagException("錯誤");
  24. }
  25. return EVAL_PAGE;
  26. }
  27. // collection只是傳遞一個標識,具體下拉值內容是從資料庫取還是從請求中得到為不同具體實現
  28. protected String collection;
  29. public String getCollection() {
  30. return collection;
  31. }
  32. public void setCollection(String collection) {
  33. this.collection = collection;
  34. }
  35. }

具體你的字典數據從資料庫中如何存儲如何查詢,可以自定義實現

顯示的標籤實現,為了將來可以在頁面取到標籤內容值,我們定義隱藏域來保存屬性值,然後在顯示顯示內容

  1. package com;
  2. import java.io.IOException;
  3. import javax.servlet.jsp.JspException;
  4. import javax.servlet.jsp.JspTagException;
  5. import javax.servlet.jsp.tagext.BodyTagSupport;
  6. @SuppressWarnings("serial")
  7. public class SelectDisplay extends BodyTagSupport {
  8. @Override
  9. public int doStartTag() throws JspException {
  10. try {
  11. StringBuffer results = new StringBuffer("");
  12. pageContext.getOut().write(results.toString());
  13. } catch (IOException ex) {
  14. throw new JspTagException("錯誤");
  15. }
  16. return EVAL_BODY_INCLUDE;
  17. }
  18. @Override
  19. public int doEndTag() throws JspException {
  20. try {
  21. StringBuffer results = new StringBuffer("");
  22. if ("SEX".equals(collection)) {
  23. results.append("<span>");
  24. results.append("<input type=\"");
  25. results.append("hidden\" name=\"");
  26. results.append(getName());
  27. results.append("\"");

  28. results.append(" value=\"");
  29. results.append(getValue());
  30. results.append("\">");
  31. if ("1".equals(getValue())) {
  32. results.append("男");
  33. } else if ("2".equals(getValue())) {
  34. results.append("女");
  35. } else {
  36. results.append("請選擇");
  37. }
  38. results.append("</span>");
  39. }
  40. pageContext.getOut().write(results.toString());
  41. } catch (IOException ex) {
  42. throw new JspTagException("錯誤");
  43. }
  44. return EVAL_PAGE;
  45. }
  46. // collection只是傳遞一個標識,具體下拉值內容是從資料庫取還是從請求中得到為不同具體實現
  47. protected String collection;
  48. // 傳遞的值
  49. protected String value;
  50. // 該屬性的名稱
  51. protected String name;
  52. public String getCollection() {
  53. return collection;
  54. }
  55. public void setCollection(String collection) {
  56. this.collection = collection;
  57. }
  58. public String getName() {
  59. return name;
  60. }
  61. public void setName(String name) {
  62. this.name = name;
  63. }
  64. public String getValue() {
  65. return value;
  66. }
  67. public void setValue(String value) {
  68. this.value = value;
  69. }
  70. }

5、JSP中引用,直接在index.jsp中引用

需要引入相應的標籤內容,引入的方式在JSP頭部引用

標籤的屬性可以設置也可以不設置,標籤的使用和HTML標籤的使用是一樣的,定義屬性即可

  1. <%@ page language="java" pageEncoding="UTF-8"%>
  2. <%@ taglib uri="/tld/web-html" prefix="html"%>
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  4. <html>
  5. <head>
  6. <title>JSP 自定義標籤的實現</title>
  7. </head>
  8. <body>
  9. 請選擇:
  10. <html:select name="sex" style="width:100px">
  11. <html:options collection="SEX"></html:options>
  12. </html:select>
  13. 顯示性別:
  14. <html:selectDisplay collection="SEX" value="1" name="sex"></html:selectDisplay>
  15. </body>
  16. </html>

6、后話

訪問項目就可以看到效果,附件是這個項目的源代碼,導入到MyEclipse中可以查看

如果想要自己設計一個大的標籤庫,可以設計一個父類,包含一些主要的屬性,例如name,id,style等屬性.然後在子類中定義自己的特有屬性

這個實現只是學習一下JSP自定義標籤使用的HelloWorld程序,然後包含了字典應用的實際例子,程序簡單,僅供參考.


[火星人 ] JSP自定義標籤實現數據字典已經有849次圍觀

http://coctec.com/docs/java/show-post-59885.html