How to get the description of an Enum in xhtml?

Asked

Viewed 1,896 times

0

I need help to resolve a situation with an Enum class along with the Dampelle framework. Ex.: I have this class: Classe EnumTipoLei

but I would like the descriptive Ex.: Ordinary Law to appear in my combobox, and not LEI_ORDINARIA. See image below: tela do cadastro

ok you must have thought, ah missed the add to "Description", but it was done too, it is not accepting to call the Description attribute of the getDescription method as per image 1, see the image below: input jsf

Error: Erro

2 answers

2

So that your <p:selectOneMenu> show the description in the options you must specify which property should be shown through the attribute itemLabel.

The standard JSF does not have the necessary support for what you need with ENUMS. Therefore, we have to resort to other support bilbiotecas. In that case, the library Primefaces Extensions has improved ENUMS support for JSF.

Using Primefaces Extensions your code would look 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>

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

More information about the component <pe:importEnum>: http://www.primefaces.org/showcase-ext/sections/utils/importEnum.jsf

  • I did as you said, but I’m having trouble importing ALL.ENUM_VALUES, the following image: https://dl.dropboxusercontent.com/u/17997689/06%20-%20cabe%C3%A7alho.png, and https://dl.dropboxusercontent.com/u/17997689/05%20-%20error%20import%C3%A7%C3%A3o%20pe.png https error://dl.dropboxusercontent.com/u/17997689/Captura%20de%20tela%202015-02-09%2008.33.35.png

  • My dear, I’m sorry. There is a part of the answer that is incorrect (no <f:selectItems>), where I wrote value="#{leiEditMD.enumTipoLei.ALL_ENUM_VALUES}" should be value="#{enum.ALL_ENUM_VALUES}".

  • did not work, the field selectOneMenu is bringing empty, follow image, https://dl.dropboxusercontent.com/u/17997689/07%20-%20selectOneMenu%20in%20white.png

  • How is your code <p:selectOneMenu>?

  • <p:selectOneMenu id="enumTipoLei" effect="fade" value="#{leiEditMB.bean.enumTipoLei}">&#xA; <f:selectItems value="#{leiEditMB.enumTipoLei}"/>&#xA; </p:selectOneMenu>

  • You have to use the imported attribute, in this case, the var="#{enum}", so try it like this: <p:selectOneMenu id="enumTipoLei" effect="fade" value="#{leiEditMB.bean.enumTipoLei}"> <f:selectItems value="#{enum.ALL_ENUM_VALUES}" var="e" itemLabel="#{e.descricao}" itemValue="#{e}"/> </p:selectOneMenu> I tested it here so it works.

Show 1 more comment

0


Try it so it’ll work out:

<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 an Enum-type List only with the get method and in the Managedbean constructor, placing the String values inside this list using Arrays, it works that is a beauty!!

Good luck!

  • ran cassioliveira, thanks, thank you very much!

  • Glad you could help. Then mark the answer as correct to help anyone researching the same problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.