users@glassfish.java.net

Enum and JSF R.I.

From: Dyego Souza Dantas Leal <dyego.leal_at_gmail.com>
Date: Fri, 09 Feb 2007 11:33:31 -0200

I'm trying to use Enum type on my JSF project:

When i submit the Page, a i get an error:

sourceId=tabelaConsultaSinalPublico:j_id_id54[severity=(ERROR 2),
summary=(tabelaConsultaSinalPublico:j_id_id54: 'Denominação' must be
convertible to an enum.), detail=(tabelaConsultaSinalPublico:j_id_id54:
'Denominação' must be convertible to an enum from the enum that contains
the constant 'Denominação'.)];|WARNING: FacesMessage(s) have been
enqueued, but may not have been displayed.


Why ?




The code in JSP:

            <h:selectOneMenu
valueChangeListener="#{ConsultaFace.typeOfConsultaChanged}"
immediate="true" onchange="submit()"
value="#{ConsultaFace.formBean.tipoAConsultar}">
                <f:selectItems
value="#{ConsultaFace.formBean.typesOfConsulta}"/>
            </h:selectOneMenu>

The ClassCode

  public SelectItem[] getTypesOfConsulta() {
       SelectItem[] retorno = new SelectItem[ConsultaType.values().length];
       int x = 0;
       for (ConsultaType contp : ConsultaType.values()) {
           retorno[x++] = new SelectItem(contp,contp.toString());
       }
       return retorno;
    }

    public void typeOfConsultaChanged(ValueChangeEvent event) {
        FacesContext context = FacesContext.getCurrentInstance();
        getFormBean().setTipoAConsultar((ConsultaType) event.getNewValue());
        context.renderResponse( );
    }
   


The Enum Type:

public enum ConsultaType {
    DENOMINACAO("Denominação"),
    TABELIAO("Tabelião"),
    ESCREVENTE("Escrevente");
   
    private static final long serialVersionUID = 1L;
    private String descricao;
   
    ConsultaType(String descricao) {
        this.descricao =descricao;
    }
   
    public String toString() {
        return descricao;
    }
}