2013-03-22 39 views
6

我使用JSF 2.Invoke方法與EL可變參數拋出java.lang.IllegalArgumentException異常:參數錯號

我有檢查從值列表匹配值的方法:

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

但它給了我一個:

嚴重[javax.enterprise.resource.webcontainer.jsf.context(HTTP-本地主機/ 127.0.0.1:8080-5)java.lang.IllegalArgumentException異常:參數

錯誤的數量是有辦法從EL執行這樣的方法?

回答

15

不,不可能在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)} 
相關問題