JSP 自定义 taglib

taglib的配置文件说明

<?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>toolLib 1.0 functions library</description>
<display-name>toolLib 1.0 functions</display-name>
<tlib-version>1.1</tlib-version>
<!--自定义的标签,就像jstl的<c:>一样-->
<short-name>auf</short-name>
<!--uri-->
<uri>http://java.sun.com/jsp/jstl/functionss</uri>

<function>
<description>
简单描述
</description>
<!--需要调用的Java的方法-->
<name>getTool</name>
<!--对应的Java文件-->
<function-class>net.cxiny.util.ToolLib</function-class>
<!--方法签名-->
<function-signature>int getTool(int)</function-signature>
<example>
示例说明
</example>
</function>

</taglib>

自定义taglib

<?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>

<!-- Encodes -->
<function>
<description>URL编码</description>
<name>urlEncode</name>
<function-class>com.jeeplus.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.jeeplus.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.jeeplus.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.jeeplus.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.jeeplus.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>replaceHtml</name>
<function-class>com.jeeplus.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.jeeplus.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.jeeplus.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.jeeplus.common.utils.DateUtils</function-class>
<function-signature>long pastDays(java.util.Date)</function-signature>
<example>${fns:pastDays(date)}</example>
</function>
<function>
<description>将日期对象Date转为标准格式字符串yyyy-mm-dd hh:mm:ss显示</description>
<name>formatDateTime</name>
<function-class>com.jeeplus.common.utils.DateUtils</function-class>
<function-signature>java.lang.String formatDateTime(java.util.Date)</function-signature>
<example>${fns:formatDateTime(date)}</example>
</function>
<!-- JsonMapper -->
<function>
<description>对象转换JSON字符串</description>
<name>toJson</name>
<function-class>com.jeeplus.common.mapper.JsonMapper</function-class>
<function-signature>java.lang.String toJsonString(java.lang.Object)</function-signature>
<example>${fns:toJson(object)}</example>
</function>

<function>
<description>根据编码获取用户对象</description>
<name>getByLoginName</name>
<function-class>com.jeeplus.modules.sys.utils.UserUtils</function-class>
<function-signature>com.jeeplus.modules.sys.entity.User getByLoginName(java.lang.String)</function-signature>
<example>${fns:getByLoginName(loginName)}</example>
</function>


<function>
<description>判断是否是图片</description>
<name>isImage</name>
<function-class>com.jeeplus.modules.aqsc.util.Utils</function-class>
<function-signature>boolean isImage(java.lang.String)</function-signature>
<example>${fns:isImage(path)}</example>
</function>
<function>
<description>获取文件缩略图</description>
<name>getFileIcon</name>
<function-class>com.jeeplus.modules.aqsc.util.Utils</function-class>
<function-signature>java.lang.String getFileIcon(java.lang.String)</function-signature>
<example>${fns:isImage(path)}</example>
</function>

</taglib>

引入自定义taglib

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

jsp中调用taglib

<function>
<description>获取字典对象列表</description>
<name>getDictList</name>
<function-class>com.jeeplus.modules.sys.utils.DictUtils</function-class>
<function-signature>java.util.List getDictList(java.lang.String)</function-signature>
<example>${fns:getDictList(type)}</example>
</function>
<c:forEach items="${fns:getDictList('act_category')}" var="dict">
<option value="${dict.value}">${dict.label}</option>
</c:forEach>



${fns:getDictLabel(model.category,'act_category','无分类')}



<fmt:formatDate value="${model.createTime}" pattern="yyyy-MM-dd HH:mm:ss"/>