【问题标题】:Passing a Enum value as a parameter from JSF (revisited)从 JSF 传递枚举值作为参数(重新访问)
【发布时间】:2011-06-27 07:11:38
【问题描述】:

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

可能是什么因素导致此示例中的行为与前面链接的行为不同?

【问题讨论】:

  • backingbean 是否有 QueryScope 的实例?看不到你的整个 backingbean 类,但我可以想象这将是 jsf 不注册枚举的原因
  • enum 定义是BackingBean 类的一部分。它本身没有 QueryScope 作为成员的实例。

标签: java jsf enums scope el


【解决方案1】:

在您的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
}

【讨论】:

    猜你喜欢
    • 2011-04-24
    • 1970-01-01
    • 2010-09-26
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多