How to display all enum values as <f:selectItems> with enum property as label

萝らか妹 提交于 2019-12-11 07:18:26

问题


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?

Here's the enum:

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;
    }

}

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;
    }

}

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?


回答1:


You don't need a converter. JSF has already a builtin enum converter.

Without OmniFaces, here's how you could provide the enum values as available items of <h:selectOneMenu>:

  1. Add this method to RoleController:

    public RoleStatus[] getRoleStatuses() {
        return RoleStatus.values();
    }
    

    This way, all enum values are available by #{roleController.roleStatuses}.

  2. Then use this dropdown:

    <h:selectOneMenu value="#{role.roleStatus}">
        <f:selectItems value="#{roleController.roleStatuses}" var="roleStatus"
            itemValue="#{roleStatus}" itemLabel="#{roleStatus.status}" />
    </h:selectOneMenu>
    

    Note: as those values are static/applicationwide, it doesn't hurt to move the method to a separate @ApplicationScoped bean.


With OmniFaces, you could remove the additional getter and just import the enum directly via <o:importConstants>:

  1. Add this somewhere in top of your template (assuming that it's in com.example package):

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

    This way, the enum class itself is available by #{RoleStatus} (note the capitalization!).

  2. Then use this dropdown:

    <h:selectOneMenu value="#{role.roleStatus}">
        <f:selectItems value="#{RoleStatus.values()}" var="roleStatus"
            itemValue="#{roleStatus}" itemLabel="#{roleStatus.status}" />
    </h:selectOneMenu>
    


来源:https://stackoverflow.com/questions/25077803/how-to-display-all-enum-values-as-fselectitems-with-enum-property-as-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!