标签:jstl
由于本人之前并没有接触过jstl标签,说来也可笑,之前一直使用struts2标签。最近项目用到jstl,所以做些记录方便以后自己查看。
jstl的强大原因之一我认为就是他的自定义函数,我们可以在前端页面直接就调用了java后台的一些常用方法,这一点真是让我爱不释手好吗。废话不多说,接下来具体看看如何使用jstl的自定义函数。
首先jstl的jar包肯定是要导的,在这就不啰嗦了。
使用jstl自定义函数要做到以下几点:
1.定义的方法必须是public的并且是static的;
2.定义相应的tld文件在WEB-INF目录或其子目录下;
3.定义相应的自定义函数库<%@ 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> <function> <description>获取管理路径</description> <name>getAdminPath</name> <function-class>com.gisquest.djgx.common.config.Global</function-class> <function-signature>java.lang.String getAdminPath()</function-signature> <example>${fns:getAdminPath()}</example> </function> <function> <description>获取网站路径</description> <name>getFrontPath</name> <function-class>com.gisquest.djgx.common.config.Global</function-class> <function-signature>java.lang.String getFrontPath()</function-signature> <example>${fns:getFrontPath()}</example> </function> <function> <description>获取网站URL后缀</description> <name>getUrlSuffix</name> <function-class>com.gisquest.djgx.common.config.Global</function-class> <function-signature>java.lang.String getUrlSuffix()</function-signature> <example>${fns:getUrlSuffix()}</example> </function> <function> <description>获取配置</description> <name>getConfig</name> <function-class>com.gisquest.djgx.common.config.Global</function-class> <function-signature>java.lang.String getConfig(java.lang.String)</function-signature> <example>${fns:getConfig(key)}</example> </function> <function> <description>获取常量</description> <name>getConst</name> <function-class>com.gisquest.djgx.common.config.Global</function-class> <function-signature>java.lang.Object getConst(java.lang.String)</function-signature> <example>${fns:getConst(key)}</example> </function> <!-- UserUtils --> <function> <description>获取当前用户对象</description> <name>getUser</name> <function-class>com.gisquest.djgx.modules.sys.utils.UserUtils</function-class> <function-signature>com.gisquest.djgx.modules.sys.entity.User getUser()</function-signature> <example>${fns:getUser()}</example> </function> <function> <description>根据ID获得用户对象</description> <name>getUserById</name> <function-class>com.gisquest.djgx.modules.sys.utils.UserUtils</function-class> <function-signature>com.gisquest.djgx.modules.sys.entity.User getUserById(java.lang.String)</function-signature> <example>${fns:getUserById(id)}</example> </function> <function> <description>获取授权用户信息</description> <name>getPrincipal</name> <function-class>com.gisquest.djgx.modules.sys.utils.UserUtils</function-class> <function-signature>com.gisquest.djgx.modules.sys.security.SystemAuthorizingRealm.Principal getPrincipal()</function-signature> <example>${fns:getPrincipal()}</example> </function> <function> <description>获取当前用户的菜单对象列表</description> <name>getMenuList</name> <function-class>com.gisquest.djgx.modules.sys.utils.UserUtils</function-class> <function-signature>java.util.List getMenuList()</function-signature> <example>${fns:getMenuList()}</example> </function> <function> <description>获取当前用户的区域对象列表</description> <name>getAreaList</name> <function-class>com.gisquest.djgx.modules.sys.utils.UserUtils</function-class> <function-signature>java.util.List getAreaList()</function-signature> <example>${fns:getAreaList()}</example> </function> <function> <description>获取当前用户的部门对象列表</description> <name>getOfficeList</name> <function-class>com.gisquest.djgx.modules.sys.utils.UserUtils</function-class> <function-signature>java.util.List getOfficeList()</function-signature> <example>${fns:getOfficeList()}</example> </function> <function> <description>获取当前用户缓存</description> <name>getCache</name> <function-class>com.gisquest.djgx.modules.sys.utils.UserUtils</function-class> <function-signature>java.lang.Object getCache(java.lang.String, java.lang.Object)</function-signature> <example>${fns:getCache(cacheName, defaultValue)}</example> </function> <!-- DictUtils --> <function> <description>获取字典标签</description> <name>getDictLabel</name> <function-class>com.gisquest.djgx.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.gisquest.djgx.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.gisquest.djgx.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> <description>获取字典对象列表</description> <name>getDictList</name> <function-class>com.gisquest.djgx.modules.sys.utils.DictUtils</function-class> <function-signature>java.util.List getDictList(java.lang.String)</function-signature> <example>${fns:getDictList(type)}</example> </function> <!-- Encodes --> <function> <description>URL编码</description> <name>urlEncode</name> <function-class>com.gisquest.djgx.common.utils.Encodes</function-class> <function-signature>java.lang.String urlEncode(java.lang.String)</function-signature> <example>${fns:urlEncode(part)}</example> </function> <function> <description>URL解码</description> <name>urlDecode</name> <function-class>com.gisquest.djgx.common.utils.Encodes</function-class> <function-signature>java.lang.String urlDecode(java.lang.String)</function-signature> <example>${fns:urlDecode(part)}</example> </function> <function> <description>HTML编码</description> <name>escapeHtml</name> <function-class>com.gisquest.djgx.common.utils.Encodes</function-class> <function-signature>java.lang.String escapeHtml(java.lang.String)</function-signature> <example>${fns:escapeHtml(html)}</example> </function> <function> <description>HTML解码</description> <name>unescapeHtml</name> <function-class>com.gisquest.djgx.common.utils.Encodes</function-class> <function-signature>java.lang.String unescapeHtml(java.lang.String)</function-signature> <example>${fns:unescapeHtml(html)}</example> </function> <!-- StringUtils --> <function> <description>从后边开始截取字符串</description> <name>substringAfterLast</name> <function-class>org.apache.commons.lang3.StringUtils</function-class> <function-signature>java.lang.String substringAfterLast(java.lang.String, java.lang.String)</function-signature> <example>${fns:substringAfterLast(str,separator)}</example> </function> <function> <description>判断字符串是否以某某开头</description> <name>startsWith</name> <function-class>org.apache.commons.lang3.StringUtils</function-class> <function-signature>boolean startsWith(java.lang.CharSequence, java.lang.CharSequence)</function-signature> <example>${fns:startsWith(str,prefix)}</example> </function> <function> <description>判断字符串是否以某某结尾</description> <name>endsWith</name> <function-class>org.apache.commons.lang3.StringUtils</function-class> <function-signature>boolean endsWith(java.lang.CharSequence, java.lang.CharSequence)</function-signature> <example>${fns:endsWith(str,suffix)}</example> </function> <function> <description>缩写字符串,超过最大宽度用“...”表示</description> <name>abbr</name> <function-class>com.gisquest.djgx.common.utils.StringUtils</function-class> <function-signature>java.lang.String abbr(java.lang.String, int)</function-signature> <example>${fns:abbr(str,length)}</example> </function> <function> <description>替换掉HTML标签后缩写字符串,超过最大宽度用“...”表示</description> <name>rabbr</name> <function-class>com.gisquest.djgx.common.utils.StringUtils</function-class> <function-signature>java.lang.String rabbr(java.lang.String, int)</function-signature> <example>${fns:rabbr(str,length)}</example> </function> <function> <description>替换掉HTML标签</description> <name>replaceHtml</name> <function-class>com.gisquest.djgx.common.utils.StringUtils</function-class> <function-signature>java.lang.String replaceHtml(java.lang.String)</function-signature> <example>${fns:replaceHtml(html)}</example> </function> <function> <description>转换为JS获取对象值,生成三目运算返回结果。</description> <name>jsGetVal</name> <function-class>com.gisquest.djgx.common.utils.StringUtils</function-class> <function-signature>java.lang.String jsGetVal(java.lang.String)</function-signature> <example>${fns:jsGetVal('row.user.id')} 返回:!row?'':!row.user?'':!row.user.id?'':row.user.id</example> </function> <!-- DateUtils --> <function> <description>获取当前日期</description> <name>getDate</name> <function-class>com.gisquest.djgx.common.utils.DateUtils</function-class> <function-signature>java.lang.String getDate(java.lang.String)</function-signature> <example>${fns:getDate(pattern)}</example> </function> <function> <description>获取过去的天数</description> <name>pastDays</name> <function-class>com.gisquest.djgx.common.utils.DateUtils</function-class> <function-signature>long pastDays(java.util.Date)</function-signature> <example>${fns:pastDays(date)}</example> </function> <!-- JsonMapper --> <function> <description>对象转换JSON字符串</description> <name>toJson</name> <function-class>com.gisquest.djgx.common.mapper.JsonMapper</function-class> <function-signature>java.lang.String toJsonString(java.lang.Object)</function-signature> <example>${fns:toJson(object)}</example> </function> </taglib>
相应后台工具代码:
package com.gisquest.djgx.modules.sys.utils; import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; import com.gisquest.djgx.common.utils.CacheUtils; import com.gisquest.djgx.common.utils.SpringContextHolder; import com.gisquest.djgx.modules.sys.dao.DictDao; import com.gisquest.djgx.modules.sys.entity.Dict; import com.google.common.collect.Lists; import com.google.common.collect.Maps; /** * 字典工具类 * @author Administrator * @version 2013-5-29 */ public class DictUtils { private static DictDao dictDao = SpringContextHolder.getBean(DictDao.class); public static final String CACHE_DICT_MAP = "dictMap"; 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; } 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()){ 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; } }前台调用:
<div class="control-group"> <label class="control-label" for="flag">定时类型:</label> <div class="controls"> <form:select path="flag" onchange="updateClass()"> <form:options items="${fns:getDictList('696d7f3d414940b79aa962b8c30f989f')}" itemLabel="label" itemValue="value" htmlEscape="false"/> </form:select> </div> </div>
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:jstl
原文地址:http://blog.csdn.net/nethackatschool/article/details/47082469