zoukankan      html  css  js  c++  java
  • JSP自定义tld方法标签

     卧槽

    我们可以通过tld文件,自定义一个方法标签,以便在页面中使用,目录通常放在WEB-INF下面的tlds文件夹:

    引入方式示例,直接在jsp上引入tld标签文件:

    <%@ taglib prefix="fns" uri="/WEB-INF/tlds/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>
    
    <!-- DictUtils -->
      <function>
        <description>获取字典标签</description>
        <name>getDictLabel</name>
        <function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class>
        <function-signature>java.lang.String getDictLabel(java.lang.String, java.lang.String, java.lang.String)</function-signature>
        <example>${fns:getDictLabel(value, type, defaultValue)}</example>  
      </function>
      
      <function>
        <description>获取字典标签(多个)</description>
        <name>getDictLabels</name>
        <function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class>
        <function-signature>java.lang.String getDictLabels(java.lang.String, java.lang.String, java.lang.String)</function-signature>
        <example>${fns:getDictLabels(values, type, defaultValue)}</example>  
      </function>
    
      <function>
        <description>获取字典值</description>
        <name>getDictValue</name>
        <function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class>
        <function-signature>java.lang.String getDictValue(java.lang.String, java.lang.String, java.lang.String)</function-signature>
        <example>${fns:getDictValue(label, type, defaultValue)}</example>  
      </function>
    注:
    function-class就是该方法的实体所在类路径,

    function-signature就是该方法的方法名,这个方法必须是个static方法。
    example就是使用方式写法示例

    后台:

    public static String getDictLabel(String value, String type, String defaultValue){
            if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)){
                for (Dict dict : getDictList(type)){
                    if (type.equals(dict.getType()) && value.equals(dict.getValue())){
                        return dict.getLabel();
                    }
                }
            }
            return defaultValue;
        }
        
        public static String getDictLabels(String values, String type, String defaultValue){
            if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(values)){
                List<String> valueList = Lists.newArrayList();
                for (String value : StringUtils.split(values, ",")){
                    valueList.add(getDictLabel(value, type, defaultValue));
                }
                return StringUtils.join(valueList, ",");
            }
            return defaultValue;
        }
    
        public static String getDictValue(String label, String type, String defaultLabel){
            if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(label)){
                for (Dict dict : getDictList(type)){
                    if (type.equals(dict.getType()) && label.equals(dict.getLabel())){
                        return dict.getValue();
                    }
                }
            }
            return defaultLabel;
        }

    JSP页面中使用:

      <td>
           ${fns:getDictLabel(preparationManage.preparationMode, 'samplingMode', '')}             
    </td>
  • 相关阅读:
    【经典】仙岛求药(一)
    6月份学习记录
    YZM的全排列
    最长公共子序列的长度
    20612统计八连块
    积木城堡
    不同组合数求和
    50136142WXY的百度地图
    50095106扔核弹
    【其他】关于海岛帝国互测系列赛总结
  • 原文地址:https://www.cnblogs.com/qingo00o/p/8989976.html
Copyright © 2011-2022 走看看