我想在jstl标签中使用StringUtils.abbreviate(),这个标签库的位置在哪里呢?
我已经在commons网站和网络上搜索了,但是找不到示例。是否有一种常见的方法可以找到标签库位置,例如在javadoc树的标准位置中?
谢谢! Nic
我想在jstl标签中使用StringUtils.abbreviate(),这个标签库的位置在哪里呢?
我已经在commons网站和网络上搜索了,但是找不到示例。是否有一种常见的方法可以找到标签库位置,例如在javadoc树的标准位置中?
谢谢! Nic
Nic,
通常没有预定义的标签来获取Apache实用程序方法,但是很容易添加自己的特殊标签定义以指向所需的方法。例如,在web.xml文件中添加本地taglib定义:
<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>
<%@ taglib prefix="mytags" uri="mytags" %>
...
<c:out value="${mytags:abbreviate(myString, 5)"/>
这样就可以了。如果您需要更多有关自定义标签的信息,可以在这里阅读:http://docs.oracle.com/javaee/5/tutorial/doc/bnalj.html
我最初也考虑使用自定义标签库。感谢您的帖子,它给了我这个想法。创建StringUtils的实例本身就可以正常工作,即使调用是静态的。如果您想要,您可以将其包装成一个两行代码的标签。
下面的示例将输出以下内容:现在是t...
<%@ tag import="org.apache.commons.lang3.StringUtils" %>
<jsp:useBean id="StringUtils" class="org.apache.commons.lang3.StringUtils" />
<c:out value="${StringUtils.abbreviate('Now is the time for all good', 8) }" />