nfv*_*loo 5 java jstl apache-commons apache-stringutils jakarta-ee
我想在 jstl 标签内使用 StringUtils.abbreviate(),是否有用于此的 taglib 位置?
我搜索了公共站点和网络,但找不到示例。是否有一种通用的方法也可以找到 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>
Run Code Online (Sandbox Code Playgroud)
您的 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>
Run Code Online (Sandbox Code Playgroud)
然后,您可以在 JSP 中使用新的自定义标记:
<%@ taglib prefix="mytags" uri="mytags" %>
...
<c:out value="${mytags:abbreviate(myString, 5)"/>
Run Code Online (Sandbox Code Playgroud)
那应该适合你。有关自定义标签的更多信息,您可以在这里阅读: http: //docs.oracle.com/javaee/5/tutorial/doc/bnalj.html