jstl自定义函数

1.定义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">
   <tlib-version>1.0</tlib-version>
   <function>  
    <description>获取字典值</description>  
    <name>getDictValue</name>  
    <function-class>com.util.DictUtils</function-class>  
    <function-signature>java.lang.String getDictValue(java.lang.String, java.lang.String)</function-signature>  
    <example>${fns:getDictValue(type, key)}</example>    
  </function>  
</taglib>

2.定义函数

public class DictUtils {
/**
* jstl自定义函数,前端页面直接调用java后台的方法
*/
public static String getDictValue(String type, String key){
 Map<String,Map<String,String>> map = new HashMap<String, Map<String,String>>();
 Map<String,String> dic = new HashMap<String,String>();
 dic.put("1", "男");
 map.put("gender", dic);
 String value = "";
 if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(key)){
  value = map.get(type).get(key);
 }
 return value;
}
}

3.页面引入tld文件

 <%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %>

4.页面调用自定义函数

<p>${fns:getDictValue("gender", "1")}</p>

猜你喜欢

转载自blog.csdn.net/qq_17037733/article/details/72513336