在标签库描述符中使用varargs [英] Using varargs in a Tag Library Descriptor

查看:64
本文介绍了在标签库描述符中使用varargs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将TLD映射到以下功能:

Is it possible to have a TLD map to the following function:

public static <T> T[] toArray(T... stuff) {
    return stuff;
}

我可以这样做:

<c:forEach items="${my:toArray('a', 'b', 'c')}"...

我尝试了以下<function-signature> s

java.lang.Object toArray( java.lang.Object... )
java.lang.Object[] toArray( java.lang.Object[] )

还有其他人,但似乎什么都没有.

And others but nothing seems to work.

推荐答案

不幸的是,这不可能. EL解析器立即将函数中的逗号解释为单独的参数,而无需检查是否有任何采用varargs的方法.最好的选择是使用JSTL fn:split().

Unfortunately that's not possible. The EL resolver immediately interprets the commas in the function as separate arguments without checking if there are any methods taking varargs. Your best bet is using JSTL fn:split() instead.

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...    
<c:forEach items="${fn:split('a,b,c', ',')}" var="item">
    ${item}<br/>
</c:forEach>

尽管EL的实现非常复杂,但它在EL中是很好的功能.

It would have been a nice feature in EL however, although implementing it would be pretty complex.

这篇关于在标签库描述符中使用varargs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆