简体   繁体   English

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

[英]Passing a Enum value as a parameter from JSF (revisited)

Passing a Enum value as a parameter from JSF 将Enum值作为参数从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: 我在我的支持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 并在我的JSF页面中通过EL使用它

<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) . 但是我必须处理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. 所以似乎JSF正在解释方法调用,好像我想调用一个String作为参数类型的方法(当然不存在) - 因此不会发生隐式转换。

What could be the factor that makes the behavior differ in this example from the aforelinked? 可能是什么因素导致这个例子的行为与前面提到的不同?

In your backingBean , you may have written a method with the enum parameter: 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
}

But, the proposed solution does not use enum, it uses String . 但是, proposed solution不使用枚举,它使用String That's because EL doesn't support enum at all: 那是因为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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM