2011-06-27 37 views
3

Passing a Enum value as a parameter from JSF传递一个枚举值从JSF参数(重新)

这个问题已经有这个问题,但所提出的解决方案并没有为我工作的交易。我在我的支持bean定义以下枚举:

public enum QueryScope { 
    SUBMITTED("Submitted by me"), ASSIGNED("Assigned to me"), ALL("All items"); 

    private final String description; 

    public String getDescription() { 
    return description; 
    } 

    QueryScope(String description) { 
    this.description = description; 
    } 
} 

然后,我把它作为一个方法参数

public void test(QueryScope scope) { 
    // do something 
} 

,并通过EL在我的JSF页面中使用它

<h:commandButton 
     id  = "commandButton_test" 
     value  = "Testing enumerations" 
     action = "#{backingBean.test('SUBMITTED')}" /> 

到目前为止非常好 - 与原始问题中提出的问题完全相同。不过,我必须处理一个javax.servlet.ServletException: Method not found: %fully_qualified_package_name%.BackingBean.test(java.lang.String)

所以看起来JSF正在解释方法调用,就好像我想调用一个String作为参数类型的方法(当然这不存在) - 因此不会发生隐式转换。

在这个例子中,与上述相关的行为有什么不同?

+0

backingbean是否有一个QueryScope实例?不能看到你的整个backingbean类,但我可以想象这将是一个原因jsf没有注册枚举 – youri

+0

'enum'定义是'BackingBean'类的一部分。它本身没有'QueryScope'作为成员的实例。 –

回答

5

在你backingBean,你可能已经编写了一个方法与enum参数:

<!-- This won't work, EL doesn't support Enum: --> 
<h:commandButton ... action="#{backingBean.test(QueryScope.SUBMITTED)}" /> 

// backingBean: 
public void test(QueryScope queryScope) { 
    // your impl 
} 

但是,在proposed solution不使用枚举,它使用String。这是因为EL根本不支持enum:

<!-- This will work, EL does support String: --> 
<h:commandButton ... action="#{backingBean.test('SUBMITTED')}" />  

// backingBean: 
public void test(String queryScopeString) { 
    QueryScope queryScope = QueryScope.valueOf(queryScopeString); 
    // your impl 
}