Java中的Taglib:带有数组参数的标签

发布于 2024-09-26 17:57:31 字数 34 浏览 1 评论 0原文

如何定义接收数组作为参数的标签?

谢谢

How can I define a tag that receives a array as a parameter?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Smile简单爱 2024-10-03 17:57:31

JSTL 做到了,您也可以。

我碰巧手边有一个 el 函数示例,然后我将粘贴 c:forEach 定义的一部分来给您一个想法:

您可以将它作为分隔字符串传递,但如果您想要一个集合或数组,您可以使用类似这样的内容:

<function>
  <name>join</name>
  <function-class>mypackage.Functions</function-class>
  <function-signature>String join(java.lang.Object, java.lang.String)</function-signature>
</function>

显然

/**
 * jstl's fn:join only works with String[].  This one is more general.
 * 
 * usage: ${nc:join(values, ", ")}
 */
public static String join(Object values, String seperator)
{
    if (values == null)
        return null;
    if (values instanceof Collection<?>)
        return StringUtils.join((Collection<?>) values, seperator);
    else if (values instanceof Object[])
        return StringUtils.join((Object[]) values, seperator);
    else
        return values.toString();
}

,如果您不想同时处理集合,则可以使用数组来代替 Object 输入。

这是 c:forEach 定义:

<tag>
    <description>
        The basic iteration tag, accepting many different
        collection types and supporting subsetting and other
        functionality
    </description>
    <name>forEach</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.core.ForEachTag</tag-class>
    <tei-class>org.apache.taglibs.standard.tei.ForEachTEI</tei-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
            Collection of items to iterate over.
        </description>
        <name>items</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
        <type>java.lang.Object</type>
    </attribute>
    ...

JSTL does it, and you can too.

I happen to have an el function example handy, and then I'll paste a portion of the c:forEach definition to give you an idea:

You could pass it as a delimited string, but if you want a collection or array, you can use something like this:

<function>
  <name>join</name>
  <function-class>mypackage.Functions</function-class>
  <function-signature>String join(java.lang.Object, java.lang.String)</function-signature>
</function>

and

/**
 * jstl's fn:join only works with String[].  This one is more general.
 * 
 * usage: ${nc:join(values, ", ")}
 */
public static String join(Object values, String seperator)
{
    if (values == null)
        return null;
    if (values instanceof Collection<?>)
        return StringUtils.join((Collection<?>) values, seperator);
    else if (values instanceof Object[])
        return StringUtils.join((Object[]) values, seperator);
    else
        return values.toString();
}

Obviously, instead of an Object input you can use an array if you don't want to handle collections as well.

Here is the c:forEach definition:

<tag>
    <description>
        The basic iteration tag, accepting many different
        collection types and supporting subsetting and other
        functionality
    </description>
    <name>forEach</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.core.ForEachTag</tag-class>
    <tei-class>org.apache.taglibs.standard.tei.ForEachTEI</tei-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
            Collection of items to iterate over.
        </description>
        <name>items</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
        <type>java.lang.Object</type>
    </attribute>
    ...
源来凯始玺欢你 2024-10-03 17:57:31

如果数组数据是字符串,您可以将值作为分隔列表传递,可能在属性中传递。

<mytag myattribute="value1,value2,value3"/>

您可以对标记主体或 jsp:param 或类似的内容执行相同的操作,但我怀疑属性方法可能最容易编码和理解。

If the array data is Strings, you could pass the values as a delimited list, maybe in an attribute.

<mytag myattribute="value1,value2,value3"/>

You could do the same with the tag body, or a jsp:param or some such, but I suspect that the attribute approach is probably easiest to code and understand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文