企業應用中大量下拉框數據來自「數據字典」(如:性別、訂單狀態、支付方式)。期望 JSP 頁面只需聲明:
<dict:select name="gender" dictCode="GENDER" value="${user.gender}"/>
自動渲染:<select name="gender"><option value="M" selected>男</option><option value="F">女</option></select>
| 標籤名 | 職責 | 對應類 |
|---|---|---|
| select | 渲染 <select> 容器,管理 name、style、事件等屬性 | SelectTag |
| options | 遍歷字典集合,渲染 <option> 列表,支持 selected 判斷 | OptionsTag |
| selectDisplay | 純顯示模式:根據 value 反查 label,不渲染下拉框 | SelectDisplayTag |
/WEB-INF/tlds/dict.tld<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1"
xmlns="https://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="https://java.sun.com/xml/ns/javaee web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>dict</short-name>
<uri>https://example.com/tags/dict</uri>
<tag>
<name>select</name>
<tag-class>com.example.tag.dict.SelectTag</tag-class>
<body-content>JSP</body-content> <!-- 允許嵌套標籤 -->
<attribute><name>name</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute>
<attribute><name>dictCode</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute>
<attribute><name>style</name><rtexprvalue>true</rtexprvalue></attribute>
<attribute><name>onchange</name><rtexprvalue>true</rtexprvalue></attribute>
</tag>
<tag>
<name>options</name>
<tag-class>com.example.tag.dict.OptionsTag</tag-class>
<body-content>empty</body-content>
<attribute><name>collection</name><required>true</required><rtexprvalue>true</rtexprvalue><type>java.util.List</type></attribute>
<attribute><name>valueField</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute>
<attribute><name>labelField</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute>
<attribute><name>selectedValue</name><rtexprvalue>true</rtexprvalue></attribute>
</tag>
<tag>
<name>selectDisplay</name>
<tag-class>com.example.tag.dict.SelectDisplayTag</tag-class>
<body-content>empty</body-content>
<attribute><name>collection</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute>
<attribute><name>name</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute>
<attribute><name>value</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute>
</tag>
</taglib>
package com.example.tag.dict;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class SelectTag extends TagSupport {
private String name;
private String dictCode;
private String style;
private String onchange;
// 字典服務 (實際應從 Spring 容器獲取,此處簡化)
private DictService dictService = DictService.getInstance();
@Override
public int doStartTag() throws JspException {
try {
// 1. 根據 dictCode 獲取字典數據
List
public class OptionsTag extends TagSupport {
private List
public class SelectDisplayTag extends TagSupport {
private List
public class DictService {
private static DictService instance = new DictService();
private Map>> cache = new ConcurrentHashMap<>();
public static DictService getInstance() { return instance; }
public List
<%@ taglib prefix="dict" uri="https://example.com/tags/dict" %>
<!-- 編輯頁:下拉選擇 -->
<dict:select name="user.gender" dictCode="GENDER" value="${user.gender}" style="width:200px"/>
<!-- 列表頁:只讀顯示 -->
<dict:selectDisplay name="user.gender" dictCode="GENDER" value="${user.gender}"/>
<!-- 手動指定選項 (不走字典服務) -->
<dict:select name="priority" style="width:150px">
<dict:options collection="${priorityList}" valueField="code" labelField="name" selectedValue="${task.priority}"/>
</dict:select>
ApplicationContextAware,從容器獲取 DictService Bean