Invoke method with varargs in EL throws java.lang.IllegalArgumentException: wrong number of arguments

后端 未结 1 1487
一个人的身影
一个人的身影 2020-11-29 11:55

I\'m using JSF 2.

I have a method that checks for matching values from a list of values:

@ManagedBean(name=\"webUtilMB\")
@ApplicationScoped
public c         


        
相关标签:
1条回答
  • 2020-11-29 12:02

    No, it is not possible to use variable arguments in EL method expressions, let alone EL functions.

    Your best bet is to create multiple different named methods with a different amount of fixed arguments.

    public static boolean isValueIn2(Integer value, Integer option1, Integer option2) {}
    public static boolean isValueIn3(Integer value, Integer option1, Integer option2, Integer option3) {}
    public static boolean isValueIn4(Integer value, Integer option1, Integer option2, Integer option3, Integer option4) {}
    // ...
    

    As a dubious alternative, you could pass a commaseparated string and split it inside the method

    #{webUtilMB.isValueIn(OtherBean.category.id, '2,3,5')}
    

    or even a string array which is created by fn:split() on a commaseparated string

    #{webUtilMB.isValueIn(OtherBean.category.id, fn:split('2,3,5', ','))}
    

    but either way, you'd still need to parse them as integer, or to convert the passed-in integer to string.

    In case you're already on EL 3.0, you could also use the new EL 3.0 collection syntax without the need for the whole EL function.

    #{[2,3,5].contains(OtherBean.category.id)}
    
    0 讨论(0)
提交回复
热议问题