我想在 jSTL 标签内使用 StringUtils.abbreviate(),是否有用于此的标签库位置?
我搜索了公共(public)站点和网络,但找不到示例。是否也有找到 taglib 位置的通用方法,也许是在 javadoc 树的标准位置??
谢谢! 尼克
最佳答案
尼克,
通常没有用于访问 apache util 方法的预定义标记,但可以轻松添加您自己的特殊标记定义以指向您想要的方法。例如,将本地 taglib 定义添加到您的 web.xml 文件中:
<jsp-config>
<taglib>
<taglib-uri>mytags</taglib-uri>
<taglib-location>/WEB-INF/jsp/mytaglib.tld</taglib-location>
</taglib>
</jsp-config>
您的 mytaglib.tld 文件将如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tlib-version>1.0</tlib-version>
<function>
<description>Exposes the abbreviate() function from Apache StringUtils</description>
<name>abbreviate</name>
<function-class>org.apache.commons.lang.StringUtils</function-class>
<function-signature>java.lang.String abbreviate(java.lang.String,
int)</function-signature>
</function>
</taglib>
在您的 JSP 中,您可以使用新的自定义标签:
<%@ taglib prefix="mytags" uri="mytags" %>
...
<c:out value="${mytags:abbreviate(myString, 5)"/>
这应该适合你。有关自定义标签的更多信息,您可以在此处阅读:http://docs.oracle.com/javaee/5/tutorial/doc/bnalj.html
关于java - apache commons lang3 stringutils taglib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14041754/