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

发布于 2024-11-17 10:36:45 字数 1094 浏览 7 评论 0原文

从 JSF 传递 Enum 值作为参数

问题已经涉及这个问题,但是提议的解决方案对我不起作用。我在支持 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
}

并通过 EL 在我的 JSF 页面中使用它

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

到目前为止一切顺利 - 与原始问题中提出的问题相同。但是,我必须处理 javax.servlet.ServletException: Method not find: %complete_qualified_pa​​ckage_name%.BackingBean.test(java.lang.String)

所以看来 JSF 正在解释方法调用,就好像我想调用一个以 String 作为参数类型的方法(当然不存在) - 因此不会发生隐式转换。

是什么因素导致该示例中的行为与上述示例不同?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安穩 2024-11-24 10:36:45

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

但是,建议的解决方案 不使用枚举,而是使用 字符串。那是因为 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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文