Passing a Enum value as a parameter from JSF (revi

2019-07-05 07:16发布

Passing a Enum value as a parameter from JSF

This question already deals with this issue, however the proposed solution has not worked for me. I define the following enumeration in my backing 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;
  }
}

Then I use it as a method parameter

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

And use it via EL in my JSF page

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

So far so good - identical to the problem posed in the original question. However I have to deal with a javax.servlet.ServletException: Method not found: %fully_qualified_package_name%.BackingBean.test(java.lang.String).

So it seems that JSF is interpreting the method call as if I would like to call a method with String as parameter type (which of course does not exist) - therefore no implicit conversion takes place.

What could be the factor that makes the behavior differ in this example from the aforelinked?

1条回答
老娘就宠你
2楼-- · 2019-07-05 07:42

In your backingBean, you may have written a method with the enum parameter:

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

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

But, the proposed solution does not use enum, it uses String. That's because EL doesn't support enum at all:

<!-- 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
}
查看更多
登录 后发表回答