Taglib in Java: tag with array parameter

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

thank

+3
source share
2 answers

JSTL does this, and you can too.

I happen to have a convenient example of an el function, and then I will insert part of the c: forEach definition to give you an idea:

You can pass it as a delimited string, but if you need a collection or an 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 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, Objectyou can use an array instead of input if you also don't want to process collections.

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>
    ...
+4
source

, , , .

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

, jsp:param , , , , .

+3

Source: https://habr.com/ru/post/1768516/


All Articles