2

Hi I am currently working on a java project (jsf) with primefaces and I am using enum, but I cannot access its values from the view with primefaces. I have temporarily solved the problem by creating a getter from the Bean and accessing the enum values, but it should work with allSuffix = "ALL_ENUM_VALUES" or ALL_VALUES by default, I don't know if it's a problem with primefaces, joinfaces or something I'm missing. I have looked into the official documentation and it should work... any solution?

my code is


<p:importEnum
        type="com.path.enumeration.AltoMedioBajo"
        var="AltoMedioBajo"
        allSuffix="ALL_ENUM_VALUES" />

                    <p:outputLabel
                        for="posibilidad"
                        value="#{informe_msg.posibilidad}" />
                    <p:selectOneMenu
                        id="posibilidad"
                        style="width: 150px"
                        value="#{informeSeguimientoDto.specification.posibilidad}">
                        <f:selectItem
                            itemLabel=""
                            itemValue="" />
                        <f:selectItems
                            value="#{AltoMedioBajo.ALL_ENUM_VALUES}"
                            var="posibilidad"
                            itemValue="#{posibilidad}"
                            itemLabel="#{peticion_msg[posibilidad.femKey]}" />
                    </p:selectOneMenu> ```


omarrero
  • 21
  • 1

2 Answers2

1

Hi this question was answered here: How to use enum values in f:selectItem(s)

With joinfaces I tested this example:

User Bean

@Named
@ApplicationScoped
public class UserBean {

    public Role[] getRoles() {
        return Role.values();
    }
}

add_user.xhtml

<p:selectOneMenu id="roleId" value="#{userBean.newUser.roleId}">
                    <f:selectItem itemValue="" itemLabel=""/>
                    <f:selectItems value="#{userBean.roles}" var="role" itemValue="#{role.getId()}" itemLabel="#{messages['role_' += role]}"/>
</p:selectOneMenu>

Role Enum with custom order, it's not mandatory you can use default .ordinal() value

public enum Role {
ASSISTANT(1),
ACCOUNTANT(4),
CASHIER(5),
ADMIN(10);

private final int id;

Role(int id) {
    this.id = id;
}

public int getId() {
    return id;
}
// method to get the Role using Id from the database
public static Role valueOf(int id) {
    return Arrays.stream(values())
            .filter(role -> role.id == id)
            .findFirst()
            .orElse(null);
}}

messages_es.properties as Resource Bundle

role_ASSISTANT=Asistente
role_ACCOUNTANT=Contador
role_CASHIER=Cajero
role_ADMIN=Administrador
stefan-dan
  • 586
  • 5
  • 19
0

As per documentation/demo, you should use a repeat tag.

Edited: Try this version:

<p:importEnum type="com.path.enumeration.AltoMedioBajo" var="AltoMedioBajo"
              allSuffix="ALL_ENUM_VALUES" />

<p:outputLabel for="posibilidad" value="#{informe_msg.posibilidad}" />
<p:selectOneMenu id="posibilidad" style="width: 150px"
                 value="#{informeSeguimientoDto.specification.posibilidad}">
    <f:selectItem />
    <ui:repeat var="posibilidad" value="#{AltoMedioBajo.ALL_ENUM_VALUES}">  
        <f:selectItem itemValue="#{posibilidad}"
                      itemLabel="#{peticion_msg[posibilidad.femKey]}"
    </ui:repeat>
Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33