JSF 2.0:使用selectOneMenu的枚举值 [英] JSF 2.0: use Enum values for selectOneMenu

查看:127
本文介绍了JSF 2.0:使用selectOneMenu的枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSF 2.0并希望用我的Enum的值填充selectOneMenu。
一个简单的例子:

I'm using JSF 2.0 and want to fill a selectOneMenu with the values of my Enum. A simple example:

// Sample Enum
public enum Gender {
  MALE("Male"),
  FEMALE("Female");

  private final String label;

  private Gender(String label) {
    this.label = label;
  }

  public String getLabel() {
    return this.label;
  }
}

不幸的是,我不能将Seam用于当前项目,它有一个很好的< s:convertEnum /> 标签完成了大部分工作。
在Seam中,要使用Enum的值,我必须编写以下标记(并创建一个提供#{genderValues} 的工厂:

Unfortunately, i can't use Seam for my current project, which had a nice <s:convertEnum/> Tag that did most of the work. In Seam, to use the values of the Enum, i had to write the following markup (and create a factory that provides the #{genderValues}:

<!-- the Seam way -->
<h:selectOneMenu id="persongender" value="#{person.gender}">
  <s:selectItems var="_gender" value="#{genderValues}"" label="#{_gender.label}"/>
  <s:convertEnum/>
</h:selectOneMenu>

结果是因为我不必在标记内明确声明枚举值。
我知道这在JSF< 2.0中并不是很容易,但是JSF2中是否有新的东西来帮助解决这个问题?或者Weld在某种程度上对此有所帮助吗?
如果JSF2中没有新内容,那么在JSF 1.2中最简单的方法是什么?

The result is that i don't have to declare the Enum values explicitely anymore inside the markup. I know that this is not very easy in JSF <2.0, but is there any new in JSF2 to help with this issue? Or does Weld help here somehow? If there is nothing new in JSF2, what's the easiest way to do it in JSF 1.2?

或者我甚至可以整合Seam JSF标签和相应的Seam类在JavaEE6-App中获得相同的功能(没有Seam容器)?

Or can i even integrate the Seam JSF tag and the corresponding classes of Seam to get the same feature in a JavaEE6-App (without the Seam container)?

推荐答案

好的,这是最后的方法:
- 在faces-config.xml中注册标准枚举转换器(可选):

Ok, here is the final way: - Register the standard enum converter in faces-config.xml (optional):

<converter>
  <converter-for-class>java.lang.Enum</converter-for-class>
  <converter-class>javax.faces.convert.EnumConverter</converter-class>
</converter>

例如,将一个函数添加到托管bean,托管bean将Enum值转换为SelectItems数组: / p>

Add a function for example to a managed bean which converts the Enum values to an array of SelectItems:

@ManagedBean
public class GenderBean {
  public SelectItem[] getGenderValues() {
    SelectItem[] items = new SelectItem[Gender.values().length];
    int i = 0;
    for(Gender g: Gender.values()) {
      items[i++] = new SelectItem(g, g.getLabel());
    }
    return items;
  }
}

然后将此函数绑定到JSF中的selectOneMenu:

Then bind this function to the selectOneMenu in JSF:

<h:selectOneMenu id="gender" value="#{person.gender}">
  <!-- use property name not method name -->
  <f:selectItems value="#{genderBean.genderValues}" />
</h:selectOneMenu>

就是这样!不是网上这个问题的第一个解释。但我认为这是最简单的&最短的一个;)

That's it! Not the first explanation for this problem on the net. But i think it's the easiest & shortest one ;)

这篇关于JSF 2.0:使用selectOneMenu的枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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