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

查看:30
本文介绍了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>

结果是我不必再在标记中明确声明 Enum 值.我知道这在 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 数组:

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天全站免登陆