在工作中,数据字典是必不可少的工具。用一张数据字典表来存储键值对,使得项目更具有隐秘性。其他表中的字段值不必再用明文来显示,可用字典表的键值来存储。同时,字典表方便我们去查询每一个编码值所对应的真实值,大大方便了我们的工作。
为方便学习,这里给出我用到的字典表
/*==============================================================*/ /* Table: DICT */ /*==============================================================*/ CREATE TABLE DICT ( ID BIGINT NOT NULL AUTO_INCREMENT, STATE VARCHAR(3) NOT NULL DEFAULT '0', DATA_KEY VARCHAR(10), DATA_VALUE VARCHAR(50), TYPE_CODE VARCHAR(50), TYPE_NAME VARCHAR(50), ORDER_NUM INT, REMARK VARCHAR(200), CREATIME DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, CREATE_USER_ID INT, UPDATIME DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, UPDATE_USER_ID INT, PRIMARY KEY (ID) ); ALTER TABLE DICT COMMENT '字典表';这是执行后的数据表 增加几条数据用于测试
为什么数据字典要用到jsp自定义标签呢?因为我们其他表里存的是编码值,如果页面查询的话还要后台查询字典表,这样每个查询接口都要加上查询字典表的代码,实在不方便。于是,我们可以设置自定义标签,通过自定义标签将编码值自动转化为真实值,减少我们的代码量。
在WEB-INF目录下添加tld文件
<?xml version="1.0" encoding="UTF-8"?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description>SPay JSP Form Tag Library</description> <tlib-version>1.0</tlib-version><!-- 代表标签库的版本号 --> <jsp-version>1.2</jsp-version><!-- 代表jsp的版本 --> <short-name>dictTag</short-name> <uri>http://www.tljc.com/dict_tag</uri> <tag> <description>Renders an HTML 'select' element. Supports databinding to the selected option.</description> <name>option</name> <tag-class>com.gzjw.persist.utils.DictTag</tag-class> <body-content>JSP</body-content> <attribute> <name>defaultValue</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>value</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>code</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>name</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>id</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>cssClass</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>styleClass</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>multiple</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>onChange</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>添加tld文件所对应的java文件DictTag.java
public class DictTag extends RequestContextAwareTag { @Autowired DictRps dictRps; private String code; private boolean defaultValue; private String value; private String name; private String id; private String cssClass; private String styleClass; private String multiple; private String onChange; public String getCssClass() { return cssClass; } public void setCssClass(String cssClass) { this.cssClass = cssClass; } public String getStyleClass() { return styleClass; } public void setStyleClass(String styleClass) { this.styleClass = styleClass; } public String getMultiple() { return multiple; } public void setMultiple(String multiple) { this.multiple = multiple; } public String getOnChange() { return onChange; } public void setOnChange(String onChange) { this.onChange = onChange; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public boolean isDefaultValue() { return defaultValue; } public void setDefaultValue(boolean defaultValue) { this.defaultValue = defaultValue; } @Override protected int doStartTagInternal() throws Exception { Dict dict = new Dict(); DictRps dictRps =(DictRps)this.getRequestContext().getWebApplicationContext().getBean(DictRps.class); List<Dict> dict_list = dictRps.findByTypeCodeAndState(Dict.STATE_NORMAL, code); JspWriter out = pageContext.getOut(); StringBuffer sb = new StringBuffer(); /* sb.append("<select name='"+this.getName()+"' id='"+this.getId()+"'"); if(!StringUtils.isEmpty(this.getCssClass())){ sb.append("class=\"" + this.getCssClass() + "\" "); } if(!StringUtils.isEmpty(this.getStyleClass())){ sb.append("style=\"" + this.getStyleClass() + "\" "); } if(!StringUtils.isEmpty(this.getMultiple())){ sb.append("multiple=\"" + this.getMultiple() + "\" "); } if(!StringUtils.isEmpty(this.getOnChange())){ sb.append("onchange=\"" + this.getOnChange() + "\" "); } sb.append(">"); if(this.isDefaultValue()){ sb.append("<option value=''>--请选择--</option>"); }*/ for(Dict dc:dict_list){ if(dc.getDataKey().equals(this.getValue())){ sb.append("<option value='"+dc.getDataKey()+"' selected='selected'>"); }else{ sb.append("<option value='"+dc.getDataKey()+"'>"); } sb.append(dc.getDataValue()+"</option>"); } /* sb.append("</select>");*/ try { out.write(sb.toString()); } catch (IOException e) { // TODO Auto-generated catch block throw new JspException(e); } return TagSupport.EVAL_PAGE; } }页面上使用刚刚定义的JSP自定义标签
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://www.tljc.com/dict_tag" prefix="dictTag" %> <select class="selectpicker form-control my-select" id="search-type" > <option value="">请选择类型</option> <dictTag:option code="EARLY_WARN_STATE" /> </select>以上就是JSP自定义标签+数据字典的全部内容,如有问题可以一起进行讨论。