如何在 f:selectItem(s) 中使用枚举值 [英] How to use enum values in f:selectItem(s)

查看:13
本文介绍了如何在 f:selectItem(s) 中使用枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 selectOneMenu 下拉菜单,以便我可以为我的问题选择一个状态.考虑到如果枚举的顺序发生变化以及列表很大会发生什么,是否可以使 f:selectItem 更加灵活?我能做得更好吗?以及是否可以自动选择"问题所在的项目?

枚举类

公共枚举状态{已提交,拒绝,得到正式认可的}

问题实体

@Enumerated(EnumType.STRING)私人状态;

JSF

<h:outputLabel for="questionStatus" value="Status"/><h:selectOneMenu id="questionStatus" value="#{bean.question.status}" ><f:selectItem itemLabel="Submitted" itemValue="0"/><f:selectItem itemLabel="Rejected" itemValue="1"/><f:selectItem itemLabel="Approved" itemValue="2"/></h:selectOneMenu><小时/>

解决方案

JSF 有一个用于 enum 的内置转换器,所以应该这样做:

@Named@ApplicationScoped公共类数据{公共状态[] getStatuses() {返回 Status.values();}}

<h:selectOneMenu value="#{bean.question.status}"><f:selectItems value="#{data.statuses}";/></h:selectOneMenu>

(注意:从 JSF 2.0 开始,不再需要提供 SelectItem[]ListT[]List 也被接受,您可以通过 var 属性访问当前项目)

如果您碰巧使用 JSF 实用程序库 OmniFaces,那么您可以使用 而不是 bean.

如果您还打算控制标签,可以将它们添加到 Status 枚举:

public enum Status {提交(提交"),拒绝(拒绝"),批准(批准");私有字符串标签;私人状态(字符串标签){this.label = 标签;}公共字符串 getLabel() {退货标签;}}

或者,更好地将枚举值设为本地化资源包的属性键(需要 EL 3.0):

在与资源包关联的属性文件中使用它#{text}

data.status.SUBMITTED = 已提交data.status.REJECTED = 拒绝data.status.APPROVED = 已批准

I want to make a selectOneMenu dropdown so I can select a status on my question. Is it possible to make the f:selectItem more flexible considering what happens if the order of the enums changes, and if the list was large? And could I do this better? And is it possible to automatically "select" the item that the question have?

Enum class

public enum Status {
    SUBMITTED,
    REJECTED,
    APPROVED
}

Question entity

@Enumerated(EnumType.STRING)
private Status status;

JSF

<div class="field">
    <h:outputLabel for="questionStatus" value="Status" />
    <h:selectOneMenu id="questionStatus" value="#{bean.question.status}" >
        <f:selectItem itemLabel="Submitted" itemValue="0" />
        <f:selectItem itemLabel="Rejected" itemValue="1" />
        <f:selectItem itemLabel="Approved" itemValue="2" />
    </h:selectOneMenu>
    <hr />
</div>

解决方案

JSF has a builtin converter for enum, so this should do:

@Named
@ApplicationScoped
public class Data {

    public Status[] getStatuses() {
        return Status.values();
    }

}

with

<h:selectOneMenu value="#{bean.question.status}" >
    <f:selectItems value="#{data.statuses}" />
</h:selectOneMenu>

(note: since JSF 2.0 there's no need anymore to provide a SelectItem[] or List<SelectItem>, a T[] and List<T> are accepted as well and you can access the current item by var attribute)

If you happen to use JSF utility library OmniFaces, then you could use <o:importConstants> instead of a bean.

<o:importConstants type="com.example.Status" />

<h:selectOneMenu value="#{bean.question.status}" >
    <f:selectItems value="#{Status}" />
</h:selectOneMenu>

If you intend to control the labels as well, you could add them to the Status enum:

public enum Status {

    SUBMITTED("Submitted"),
    REJECTED("Rejected"),
    APPROVED("Approved");

    private String label;

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

    public String getLabel() {
        return label;
    }

}

with

<f:selectItems value="#{data.statuses}" var="status"
    itemValue="#{status}" itemLabel="#{status.label}" />

Or, better, make the enum value a property key of a localized resource bundle (EL 3.0 required):

<f:selectItems value="#{data.statuses}" var="status"
    itemValue="#{status}" itemLabel="#{text['data.status.' += status]}" />

with this in a properties file associated with resource bundle #{text}

data.status.SUBMITTED = Submitted
data.status.REJECTED = Rejected
data.status.APPROVED = Approved

这篇关于如何在 f:selectItem(s) 中使用枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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