How to get the description of an Enum in xhtml?

0

I need help to solve a situation with an Enum class together with the demoiselle framework. Eg: I have this class:

ButIwouldliketoseeinmycomboboxthedescriptiveEx.:OrdinaryLaw,notLEI_ORDINARIA.Seetheimagebelow:

ok you should have thought, ah missed the add "description", but it was done too, it is not accepting call the description attribute of the getDescricao method as image 1 , see the image below:

Error:

    
asked by anonymous 06.02.2015 / 17:59

2 answers

0

Try to get it right:

<p:selectOneMenu id="enumTipoLei" value="#{leiEditMB.bean.enumTipoLei}">
    <f:selectItems value="#{leiEditMB.enumTipoLei}" var="tipoLei" itemLabel="#{tipoLei.descricao}" itemValue="#{tipoLei}"/>
</p:selectOneMenu>

No ManagedBean:

private List<EnumTipoLei> enumTipoLei;

public LeiEditMB(){
    enumTipoLei = Arrays.asList(EnumTipoLei.values());
}

public List<EnumTipoLei> getEnumTipoLei() {
    return enumTipoLei;
}

In short: creating a List of the Enum type only with the get method and in the ManagedBean constructor, putting the String values within that list using Arrays, works that is a beauty !!

Good luck!

    
21.03.2015 / 10:25
2

In order for your <p:selectOneMenu> item to show the description in the options you must specify which property should be shown by the itemLabel attribute.

Standard JSF does not have the necessary support for what you need with ENUMS. So we have to turn to other support libraries. In this case, the PrimeFaces Extensions library has enhanced ENUMS support for JSF.

Using PrimeFaces Extensions your code looks like this:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:pe="http://primefaces.org/ui/extensions"> <!-- Importação da biblioteca no XHTML -->

<pe:importEnum type="leiEditMB.enumTipoLei" var="enum" allSuffix="ALL_ENUM_VALUES" />

<p:selectOneMenu  value="#{leiEditMB.bean.enumTipoLei}">
    <f:selectItens value="#{enum.ALL_ENUM_VALUES}" var="e"
                  itemLabel="#{e.descricao}" itemValue="#{e}" />
</p:selectOneMenu>

ENUM values can be accessed through the class name (default configuration) or through a custom name (attribute var , in code above var="enum" ). It is also possible to get all the ENUMS of the class with the suffix "ALL_VALUES" (default) or a custom prefix through the allSuffix attribute (in the above code I used allSuffix="ALL_ENUM_VALUES" ).

More information about the <pe:importEnum> component: link

    
06.02.2015 / 21:17