Passing a Enum value as a parameter from 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
}
并在我的 JSF 页面中通过 EL 使用它
<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 作为参数类型(当然不存在)的方法 - 因此没有发生隐式转换。
导致此示例中的行为与前面链接的行为不同的因素可能是什么?
最佳答案
在您的 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
}
但是,建议的解决方案
不使用枚举,而是使用 String
。那是因为 EL 根本不支持枚举:
<!-- 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
}
关于java - 从 JSF 传递枚举值作为参数(重新访问),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6489676/