欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JSP 自定義標(biāo)簽實(shí)現(xiàn)數(shù)據(jù)字典的實(shí)例

 更新時(shí)間:2017年08月21日 16:14:22   投稿:lqh  
這篇文章主要介紹了JSP 自定義標(biāo)簽實(shí)現(xiàn)數(shù)據(jù)字典的實(shí)例的相關(guān)資料,這里實(shí)現(xiàn)這樣的功能,希望能幫助到大家,需要的朋友可以參考下

JSP 自定義標(biāo)簽實(shí)現(xiàn)數(shù)據(jù)字典的實(shí)例

 1.關(guān)于JSP標(biāo)簽的好處就不再羅嗦

數(shù)據(jù)字典就是使用的下拉框,只要定義使用那個(gè)字典就會(huì)將這個(gè)字典可用的內(nèi)容顯示出來(lái)

顯示字典時(shí)只要定義那個(gè)字典和屬性值就可以顯示出字典的顯示值 

2.首先在web.xml中定義自定義標(biāo)簽加載的引用,兩個(gè)屬性分別是引用的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中定義自己的標(biāo)簽,數(shù)據(jù)字典應(yīng)用的話我們需要一個(gè)標(biāo)簽庫(kù),三個(gè)標(biāo)簽。分別是,select標(biāo)簽,options標(biāo)簽,和現(xiàn)實(shí)數(shù)據(jù)字典的標(biāo)簽,每個(gè)標(biāo)簽都對(duì)應(yīng)不同的實(shí)現(xiàn)類

<?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><!-- 標(biāo)簽庫(kù)版本 --> 
  <jsp-version>1.2</jsp-version> <!-- 標(biāo)簽庫(kù)要求的JSP規(guī)范版本 --> 
  <short-name>html</short-name>  <!-- JSP頁(yè)面編寫工具可以用來(lái)創(chuàng)建助記名的可選名字 --> 
  <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.實(shí)現(xiàn)類

實(shí)現(xiàn)類的作用就是在后臺(tái)拼接所需HTML標(biāo)簽內(nèi)容,然后由JSP進(jìn)行輸出

實(shí)現(xiàn)類最主要的兩個(gè)方法,一個(gè)遇到這個(gè)標(biāo)簽開始時(shí)輸出,一個(gè)是結(jié)束時(shí)輸出

如果需要定義屬性,可以參考實(shí)現(xiàn)類定義屬性,并在TLD中定義,在JSP中使用標(biāo)簽時(shí)快捷鍵就可以出來(lái)這個(gè)屬性

首先是select標(biāo)簽的代碼:

package com; 
import java.io.IOException; 
import javax.servlet.jsp.JspException; 
import javax.servlet.jsp.JspTagException; 
import javax.servlet.jsp.tagext.BodyTagSupport; 
/** 
 * TagSupport與BodyTagSupport的區(qū)別: 
 * 主要看標(biāo)簽處理類是否要讀取標(biāo)簽體的內(nèi)容和改變標(biāo)簽體返回的內(nèi)容,如果不需要就用TagSupport,否則就用BodyTagSupport 
 * 用TagSupport實(shí)現(xiàn)的標(biāo)簽,都可以用BodyTagSupport來(lái)實(shí)現(xiàn),因?yàn)锽odyTagSupport繼承了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("錯(cuò)誤"); 
    } 
    return EVAL_BODY_INCLUDE; 
  } 
  @Override 
  public int doEndTag() throws JspException { 
    try { 
      StringBuffer results = new StringBuffer(""); 
      // 因?yàn)橄吕邪吕瓋?nèi)容,所以只能在遇到結(jié)束標(biāo)簽時(shí)才能寫select結(jié)束 
      results.append("</select>");      
      pageContext.getOut().write(results.toString()); 
    } catch (IOException ex) { 
      throw new JspTagException("錯(cuò)誤"); 
    } 
    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()方法是遇到標(biāo)簽開始時(shí)會(huì)呼叫的方法,其合法的返回值是EVAL_BODY_INCLUDE與SKIP_BODY,前者表示將顯示標(biāo)簽間的文字,后者表示不顯示標(biāo)簽間的文字 
  doEndTag()方法是在遇到標(biāo)簽結(jié)束時(shí)呼叫的方法,其合法的返回值是EVAL_PAGE與SKIP_PAGE,前者表示處理完標(biāo)簽后繼續(xù)執(zhí)行以下的JSP網(wǎng)頁(yè),后者是表示不處理接下來(lái)的JSP網(wǎng)頁(yè) 
  doAfterBody(),這個(gè)方法是在顯示完標(biāo)簽間文字之后呼叫的,其返回值有EVAL_BODY_AGAIN與SKIP_BODY,前者會(huì)再顯示一次標(biāo)簽間的文字,后者則繼續(xù)執(zhí)行標(biāo)簽處理的下一步 
  EVAL_BODY_INCLUDE:把Body讀入存在的輸出流中,doStartTag()函數(shù)可用 
  EVAL_PAGE:繼續(xù)處理頁(yè)面,doEndTag()函數(shù)可用 
  SKIP_BODY:忽略對(duì)Body的處理,doStartTag()和doAfterBody()函數(shù)可用 
  SKIP_PAGE:忽略對(duì)余下頁(yè)面的處理,doEndTag()函數(shù)可用 
  EVAL_BODY_BUFFERED:申請(qǐng)緩沖區(qū),由setBodyContent()函數(shù)得到的BodyContent對(duì)象來(lái)處理tag的body,如果類實(shí)現(xiàn)了BodyTag,那么doStartTag()可用,否則非法 
  EVAL_BODY_AGAIN:請(qǐng)求繼續(xù)處理body,返回自doAfterBody(),這個(gè)返回值在你制作循環(huán)tag的時(shí)候是很有用的  
  預(yù)定的處理順序是:doStartTag()返回SKIP_BODY,doAfterBodyTag()返回SKIP_BODY,doEndTag()返回EVAL_PAGE 
  如果繼承了TagSupport之后,如果沒(méi)有改寫任何的方法,標(biāo)簽處理的執(zhí)行順序是:doStartTag() ->不顯示文字 ->doEndTag()->執(zhí)行接下來(lái)的網(wǎng)頁(yè) 
  如果您改寫了doStartTag(),則必須指定返回值, 
  如果指定了EVAL_BODY_INCLUDE,則執(zhí)行順序是:doStartTag()->顯示文字->doAfterBodyTag()->doEndTag()->執(zhí)行下面的網(wǎng)頁(yè) 
   */ 
} 

關(guān)于返回參數(shù),返回具體數(shù)字也可以,不用過(guò)于糾結(jié)

然后是下拉內(nèi)容實(shí)現(xiàn)類

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\">請(qǐng)選擇</option>"); 
        results.append("<option value=\"1\">男</option>"); 
        results.append("<option value=\"2\">女</option>"); 
      } 
      pageContext.getOut().write(results.toString()); 
    } catch (IOException ex) { 
      throw new JspTagException("錯(cuò)誤"); 
    } 
    return EVAL_PAGE; 
  } 
  // collection只是傳遞一個(gè)標(biāo)識(shí),具體下拉值內(nèi)容是從數(shù)據(jù)庫(kù)取還是從請(qǐng)求中得到為不同具體實(shí)現(xiàn) 
  protected String collection; 
  public String getCollection() { 
    return collection; 
  } 
  public void setCollection(String collection) { 
    this.collection = collection; 
  } 
} 

 具體你的字典數(shù)據(jù)從數(shù)據(jù)庫(kù)中如何存儲(chǔ)如何查詢,可以自定義實(shí)現(xiàn)

顯示的標(biāo)簽實(shí)現(xiàn),為了將來(lái)可以在頁(yè)面取到標(biāo)簽內(nèi)容值,我們定義隱藏域來(lái)保存屬性值,然后在顯示顯示內(nèi)容


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("錯(cuò)誤"); 
    } 
    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("請(qǐng)選擇"); 
        } 
        results.append("</span>"); 
      } 
      pageContext.getOut().write(results.toString()); 
    } catch (IOException ex) { 
      throw new JspTagException("錯(cuò)誤"); 
    } 
    return EVAL_PAGE; 
  } 
  // collection只是傳遞一個(gè)標(biāo)識(shí),具體下拉值內(nèi)容是從數(shù)據(jù)庫(kù)取還是從請(qǐng)求中得到為不同具體實(shí)現(xiàn) 
  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中引用

需要引入相應(yīng)的標(biāo)簽內(nèi)容,引入的方式在JSP頭部引用

標(biāo)簽的屬性可以設(shè)置也可以不設(shè)置,標(biāo)簽的使用和HTML標(biāo)簽的使用是一樣的,定義屬性即可

<%@ 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 自定義標(biāo)簽的實(shí)現(xiàn)</title> 
  </head> 
  <body> 
    請(qǐng)選擇: 
    <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.后話

訪問(wèn)項(xiàng)目就可以看到效果,附件是這個(gè)項(xiàng)目的源代碼,導(dǎo)入到MyEclipse中可以查看

如果想要自己設(shè)計(jì)一個(gè)大的標(biāo)簽庫(kù),可以設(shè)計(jì)一個(gè)父類,包含一些主要的屬性,例如name,id,style等屬性。然后在子類中定義自己的特有屬性

這個(gè)實(shí)現(xiàn)只是學(xué)習(xí)一下JSP自定義標(biāo)簽使用的HelloWorld程序,然后包含了字典應(yīng)用的實(shí)際例子,程序簡(jiǎn)單,僅供參考

 如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論