从JSF传递枚举值作为参数(重新访问) [英] Passing a Enum value as a parameter from JSF (revisited)

查看:135
本文介绍了从JSF传递枚举值作为参数(重新访问)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将枚举值作为参数从JSF传递



这个问题已经解决了这个问题,但是提出的解决方案对我来说没有效果。我在支持bean中定义了以下枚举:

  public enum QueryScope {
SUBMITTED(由我提交) ,ASSIGNED(分配给我),ALL(所有项目);

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 =测试枚举
action =#{backingBean.test('SUBMITTED')}/>

迄今为止,与原始问题提出的问题相同。但是我必须处理一个 javax.servlet.ServletException:找不到方法:%fully_qualified_pa​​ckage_name%.BackingBean.test(java.lang.String)



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



在这个例子中,与上述相关的行为有什么不同的因素?

解决方案您可以在 backingBean 中编写一个方法,其中包含枚举参数:

 <! - 这不行,EL不支持枚举: - > 
< h:commandButton ... action =#{backingBean.test(QueryScope.SUBMITTED)}/>

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

但是,提出的解决方案不使用枚举,它使用 String 。这是因为EL根本不支持枚举:

 <! - 这将工作,EL支持String: - > 
< h:commandButton ... action =#{backingBean.test('SUBMITTED')}/>

// backingBean:
public void test(String queryScopeString){
QueryScope queryScope = QueryScope.valueOf(queryScopeString);
// your impl
}


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?

解决方案

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
}

这篇关于从JSF传递枚举值作为参数(重新访问)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆