将Enum值作为参数从JSF传递(重新访问)

Sim*_*der 3 java jsf enums scope el

将Enum值作为参数从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;
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我用它作为方法参数

public void test(QueryScope scope) {
  // do something
}
Run Code Online (Sandbox Code Playgroud)

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

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

到目前为止一直很好 - 与原始问题中提出的问题相同.但是我必须处理一个javax.servlet.ServletException: Method not found: %fully_qualified_package_name%.BackingBean.test(java.lang.String).

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

可能是什么因素导致这个例子的行为与前面提到的不同?

fal*_*lla 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
}
Run Code Online (Sandbox Code Playgroud)

但是,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
}
Run Code Online (Sandbox Code Playgroud)