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

时间:2011-06-27 07:11:39

标签: java jsf enums scope el

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作为参数类型的方法(当然不存在) - 因此不会发生隐式转换。

这个例子与前面提到的行为有什么不同?

1 个答案:

答案 0 :(得分: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根本不支持枚举:

<!-- 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
}