问题描述
我有一个 RoleStatus
枚举,它作为一个属性 Role
实体映射到DB中的一个整数(但那部分是无关紧要的)。我想在< p:dataTable>
中呈现一个列表< Role>
,其中一列应对 Role $的
RoleStatus
属性有< h:selectOneMenu>
c $ c>实体。如何在中执行此操作?
I have a RoleStatus
enum which is as being a property of Role
entity mapped to an integer in the DB (but that part is irrelevant). I'd like to present a List<Role>
in a <p:dataTable>
in which one column should have a <h:selectOneMenu>
for the RoleStatus
property of the Role
entity. How can I implement this with or without OmniFaces?
这是枚举:
public enum RoleStatus {
ACTIVE(1, "Active"),
DISABLE(2, "Disable");
private final int intStatus;
private final String status;
private RoleStatus(int intStatus, String status) {
this.intStatus = intStatus;
this.status = status;
}
public int getIntStatus() {
return status;
}
public String getStatus() {
return status;
}
}
这是支持bean: p>
Here's the backing bean:
@ManagedBean
@ViewScoped
public class RoleController {
private List<Role> roles;
@ManagedProperty("#{roleService}")
private IRoleService roleService;
@PostConstruct
public void init() {
roles = roleService.getRoles();
}
public List<Role> getRoles() {
return roles;
}
}
最后,数据表中我对于 RoleStatus
属性角色,希望有一个
实体,显示所有可用的枚举值作为选择项目选项。< h:selectOneMenu>
Finally, the data table where I'd like to have a <h:selectOneMenu>
for the RoleStatus
property of Role
entity, showing all available enum values as select item options.
<h:form id="roleForm">
<p:dataTable value="#{roleController.roles}" var="role">
<p:column>
<h:outputText value="#{role.roleid}" />
</p:column>
<p:column>
<h:inputText value="#{role.role}" />
</p:column>
<p:column>
<h:inputText value="#{role.description}" />
</p:column>
<p:column>
<h:selectOneMenu value="#{role.roleStatus}">
<!-- How??? -->
</h:selectOneMenu>
</p:column>
</p:dataTable>
</h:form>
我该如何实现?我需要?
How can I achieve this? Do I need the OmniFaces SelectItemsConverter
?
推荐答案
您不需要转换器。 JSF已经有一个内置的枚举转换器。
You don't need a converter. JSF has already a builtin enum converter.
没有OmniFaces,您可以将枚举值提供为< h:selectOneMenu>
的可用项:
Without OmniFaces, here's how you could provide the enum values as available items of <h:selectOneMenu>
:
-
将此方法添加到
RoleController
:
public RoleStatus[] getRoleStatuses() {
return RoleStatus.values();
}
这样,所有枚举值都可以通过# {roleController.roleStatuses}
。
This way, all enum values are available by #{roleController.roleStatuses}
.
然后使用此下拉列表:
<h:selectOneMenu value="#{role.roleStatus}">
<f:selectItems value="#{roleController.roleStatuses}" var="roleStatus"
itemValue="#{roleStatus}" itemLabel="#{roleStatus.status}" />
</h:selectOneMenu>
注意:由于这些值为静态/全局应用,因此不会将方法移动到单独的 @ApplicationScoped
bean。
Note: as those values are static/applicationwide, it doesn't hurt to move the method to a separate @ApplicationScoped
bean.
使用OmniFaces,您可以删除额外的getter,并直接通过:
With OmniFaces, you could remove the additional getter and just import the enum directly via <o:importConstants>
:
-
您的模板的顶部(假设它在
com.example
包中):
<o:importConstants type="com.example.RoleStatus" />
这样,枚举类本身可以通过#{RoleStatus}
(注意大写!)
This way, the enum class itself is available by #{RoleStatus}
(note the capitalization!).
然后使用此下拉列表:
<h:selectOneMenu value="#{role.roleStatus}">
<f:selectItems value="#{RoleStatus.values()}" var="roleStatus"
itemValue="#{roleStatus}" itemLabel="#{roleStatus.status}" />
</h:selectOneMenu>
这篇关于如何将所有枚举值显示为< f:selectItems>枚举属性作为标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!