gpt4 book ai didi

jsf-2 - 在 EL 中使用可变参数调用方法抛出 java.lang.IllegalArgumentException : wrong number of arguments

转载 作者:行者123 更新时间:2023-12-04 13:19:58 24 4
gpt4 key购买 nike

我正在使用 JSF 2。

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

@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 中调用此方法,我尝试过:
#{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: wrong number of arguments



有没有办法从 EL 执行这样的方法?

最佳答案

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

最好的办法是使用不同数量的固定参数创建多个不同的命名方法。

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) {}
// ...

作为一个可疑的替代方案,您可以传递一个逗号分隔的字符串并将其拆分到方法中

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

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

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

但无论哪种方式,您仍然需要将它们解析为整数,或者将传入的整数转换为字符串。

如果您已经使用 EL 3.0,您还可以使用新的 EL 3.0 collection syntax不需要整个 EL 功能。
#{[2,3,5].contains(OtherBean.category.id)}

关于jsf-2 - 在 EL 中使用可变参数调用方法抛出 java.lang.IllegalArgumentException : wrong number of arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15560508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com