在 EL 中使用可变参数调用方法抛出 java.lang.IllegalArgumentException:参数数量错误 [英] Invoke method with varargs in EL throws java.lang.IllegalArgumentException: wrong number of arguments

查看:20
本文介绍了在 EL 中使用可变参数调用方法抛出 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)}

但它给了我一个:

SEVERE [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,您还可以使用新的 EL 3.0 集合语法 不需要整个 EL 函数.

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 中使用可变参数调用方法抛出 java.lang.IllegalArgumentException:参数数量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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