java自定义标签,tld文件,获取数据字典的值

java web日常开发中,jsp页面经常会取数据字典的值,比如订单状态:审核中、已通过、不通过;学历:初中、高中、大学、研究生等等。通常情况下我们是将这种常量数据配置成数据字典,取得时候直接通过自定义标签来获取。首先我们会创建一个tld文件,放在项目的WEB-INFO文件夹下面,主要声明处理类和处理方法以及自定的标签名称。类似下面的这个文件:

<?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>JSTL 1.1 functions library</description>  
  <display-name>JSTL functions sys</display-name>  
  <tlib-version>1.1</tlib-version>  
  <short-name>fns</short-name>  
  <uri>/fns-tags</uri> 
  
  <function>
      <description>获取字典表值</description>
      <name>getDictLabel</name>
      <function-class>com.test.DictUtil</function-class>
      <function-signature>java.lang.String getDictLabel(java.lang.String ,java.lang.String)</function-signature>
      <example>${fns:getDictLabel(type,code)}</example>
  </function>
  
  <function>
      <description>获取字典表值列表</description>
      <name>getDictList</name>
      <function-class>com.test.DictUtil</function-class>
      <function-signature>java.util.List getDictList(java.lang.String)</function-signature>
      <example>${fns:getDictList(type)}</example>
  </function>
  
</taglib>

上面的tld文件主要定义了两个标签,使用方法分别为:

获取数据字典的值   ${fns:getDictLabel(type,code)},
获取数据字典列表 ${fns:getDictList(type)}

这两个标签对应的标签类为com.test.DictUtil,主要有两个方法,如下:

package com.test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.test.entity.dict.DictBase;
import com.yifen.util.StringUtil;import com.test.service.dict.DictService;

/**
 * 自定义标签取值*/
@Component
public class DictUtil {

    @Autowired(required = false)
    private DictService<DictBase> dictService;private static DictUtil dictUtil;

    @PostConstruct
    public void init() {
        dictUtil = this;
        dictUtil.dictService = this.dictService;
    }

    /**
     * 根据类型type和编码code获取唯一的字典值
     * 
     * @param type
     * @param code
     * @return
     */
    public static String getDictLabel(String type, String code) {
        String result = "-";
        DictBase tempDict = null;
        DictBase dictBase = new DictBase();
        dictBase.setDictType(type);
        dictBase.setDictValue(code);
        if (!"".equals(code) && !"".equals(type)) {
            tempDict = dictBaseUtil.dictService.getDictLabel(dictBase);
        }
        if (tempDict != null) {
            result = tempDict.getDictName();
        }
        return result;
    }/**
     * 根据类型type获取字典列表
     * 
     * @param type
     * @param code
     * @return
     */
    public static List<DictBase> getDictList(String type) {
        List<DictBase> result = null;
        if (!StringUtil.isEmpty(type)) {
            result = dictBaseUtil.dictService.getDictList(type);
        }
        if (result == null) {
            result = new ArrayList<DictBase>();
        }
        return result;
    }
}

然后在jsp文件的中应用一下tld文件就可以直接使用该标签获取数据字典的值了,下面例子为页面获取一个订单状态下拉框,该订单状态就是配置到数据字典的

/**引用tld文件**/
<%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld"%>
/**使用方式**/
<div class="input-group input-group-sm w200"> <span class="input-group-addon">订单状态:</span> <select id="payType" name="payType" class="form-control"> <option value="">请选择</option> <c:forEach var="item" items="${fns:getDictList('order_status') }"> <option value="${item.dictValue }"<c:if test="${item.dictValue == entity.orderStatus}">selected="selected"</c:if>>${item.dictName }</option> </c:forEach> </select> </div>

原文地址:https://www.cnblogs.com/mark8080/p/7511229.html