Passing a Enum value as a parameter from JSF (revisited)

跟風遠走 提交于 2019-12-12 16:18:32

问题


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:


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
}


来源:https://stackoverflow.com/questions/6489676/passing-a-enum-value-as-a-parameter-from-jsf-revisited

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!