數據字典就是使用的下拉框,只要定義使用那個字典就會將這個字典可用的內容顯示出來.本文將講解怎麼自定義標籤實現數據字典.
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;
- @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程序,然後包含了字典應用的實際例子,程序簡單,僅供參考.
[火星人 ] JSP自定義標籤實現數據字典已經有799次圍觀