1. 程式人生 > >關於jeesite字典表的實現,el自定義方法tld

關於jeesite字典表的實現,el自定義方法tld

用jeesite搭建專案發現字典表非常好用,速度的轉義很快,看了下程式碼,簡單的解釋下,發現是是使用了el自定義方法tld
1:在jsp的獲取方式${fns:getDictList('yes_no')}
 
<div class="control-group">
            <label class="control-label">是否允許登入:</label>
            <div class="controls">
                <form:select path="loginFlag">
                    <form:options items="${fns:getDictList('yes_no')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
                </form:select>
                <span class="help-inline"><font color="red">*</font> “是”代表此賬號允許登入,“否”則表示此賬號不允許登入</span>
            </div>
</div>


2:使用fns:要引入fns,用到el 自定義方法 tld
<%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %>
3:其實uri中寫了fns.tld檔案的位置,這個檔案中有該方法的自定義函式
fns.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>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>http://java.sun.com/jsp/jstl/functionss</uri>
  <function>
    <description>獲取字典物件列表</description>
    <name>getDictList</name>
    <function-class>com.thinkgem.jeesite.modules.sys.utils.DictUtils</function-class>
    <function-signature>java.util.List getDictList(java.lang.String)</function-signature>
    <example>${fns:getDictList(type)}</example>  
  </function>
</taglib>
寫在<function>中<function-class>寫了class類<function-signature>寫了類裡面對應的方法
4.後臺寫對應的類及方法:特別注意一定為靜態方法
public class DictUtils {
    private static DictDao dictDao = SpringContextHolder.getBean(DictDao.class);
    public static final String CACHE_DICT_MAP = "dictMap";
    public static 
List<Dict> getDictList(String type){ @SuppressWarnings("unchecked") Map<String, List<Dict>> dictMap = (Map<String, List<Dict>>)CacheUtils.get(CACHE_DICT_MAP); if (dictMap==null){ dictMap = Maps.newHashMap(); for (Dict dict : dictDao.findAllList(new Dict())){ List<Dict> dictList = dictMap.get(dict.getType()); if (dictList != null){ dictList.add(dict); }else{ dictMap.put(dict.getType(), Lists.newArrayList(dict)); } } CacheUtils.put(CACHE_DICT_MAP, dictMap); } List<Dict> dictList = dictMap.get(type); if (dictList == null){ dictList = Lists.newArrayList(); } return dictList; } }
也會發現使用了快取,大大提高轉換效率