如何在f中使用枚举值:selectItem(s) [英] How to use enum values in f:selectItem(s)
问题描述
我想做一个selectOneMenu下拉列表,所以我可以选择我的问题的状态。是否可以使f:selectItem更灵活,考虑到枚举的顺序如何变化会发生什么,如果列表很大?我能做得更好吗是否可以自动选择该问题的项目?
枚举类
public enum Status {
SUBMITTED,
REJECTED,
APPROVED
}
问题实体
@Enumerated(EnumType.STRING)
private状态;
JSF
< div class =field>
< h:outputLabel for =questionStatusvalue =Status/>
< h:selectOneMenu id =questionStatusvalue =#{bean.question.status}>
< f:selectItem itemLabel =已提交itemValue =0/>
< f:selectItem itemLabel =被拒绝itemValue =1/>
< f:selectItem itemLabel =已批准itemValue =2/>
< / h:selectOneMenu>
< hr />
< / div>
内置转换器为枚举
,所以应该这样做:
@ManagedBean
@ApplicationScoped
public class Data {
public Status [] getStatuses(){
return Status.values();
}
}
with
< h:selectOneMenu value =#{bean.question.status}>
< f:selectItems value =#{data.statuses}/>
< / h:selectOneMenu>
(注意:由于JSF 2.0不再需要提供 SelectItem []
或列表< SelectItem>
, T []
和列表< T>
也被接受,您可以通过 var
属性访问当前项目
如果您碰巧使用JSF实用程序库 OmniFaces ,那么您可以使用<一个href =http://showcase.omnifaces.org/taghandlers/importConstants =noreferrer> < o:importConstants>
而不是bean。
< o:importConstants type =com.example.Status/>
< h:selectOneMenu value =#{bean.question.status}>
< f:selectItems value =#{Status}/>
< / h:selectOneMenu>
如果您打算控制标签,可以将它们添加到状态
枚举:
public enum Status {
提交(已提交),
REJECTED(被拒绝),
已批准(已批准);
private String label;
私人状态(String label){
this.label = label;
}
public String getLabel(){
return label;
}
}
with
< f:selectItems value =#{data.statuses}var =status
itemValue =#{status}itemLabel =#{status.label}/>或者更好的是,使枚举值成为本地化资源束的属性键(需要EL 3.0) : < f:selectItems value =#{data.statuses}var =状态
itemValue =#{status}itemLabel =#{text ['data.status。'+ = status]}/>
在与资源束相关联的属性文件中#{text}
data.status.SUBMITTED =提交
数据。 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:
@ManagedBean
@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屋!