EL中使用varargs的调用方法将引发java.lang.IllegalArgumentException:参数数量错误 [英] Invoke method with varargs in EL throws java.lang.IllegalArgumentException: wrong number of arguments

查看:150
本文介绍了EL中使用varargs的调用方法将引发java.lang.IllegalArgumentException:参数数量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSF 2.

I'm using JSF 2.

我有一种方法可以检查值列表中的匹配值:

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

@ManagedBean(name="webUtilMB")
@ApplicationScoped
public class WebUtilManagedBean implements Serializable{ ...

public static boolean isValueIn(Integer value, Integer ... options){
    if(value != null){
        for(Integer option: options){
            if(option.equals(value)){
                return true;
            }
        }
    }
    return false;
}


...
}

要在EL中调用此方法,我尝试过:

To call this method in EL I tried:

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

但是它给了我一个

严重[javax.enterprise.resource.webcontainer.jsf.context](http-localhost/127.0.0.1:8080-5)java.lang.IllegalArgumentException:参数数量错误

SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (http-localhost/127.0.0.1:8080-5) java.lang.IllegalArgumentException: wrong number of arguments

有没有一种方法可以从EL中执行这种方法?

Is there a way to execute such a method from EL?

推荐答案

否,不能在EL方法表达式中使用变量参数,更不用说EL函数了.

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')}

,甚至是由fn:split()在逗号分隔的字符串上创建的字符串数组

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.

如果您已经在使用EL 3.0,还可以使用新的

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)}

这篇关于EL中使用varargs的调用方法将引发java.lang.IllegalArgumentException:参数数量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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