First faces problem when converting an Enum

Asked

Viewed 266 times

0

I’m having trouble converting an I made a class to convert only at the time of subtemer the form happens the following error:

javax.faces.FacesException: java.lang.IllegalArgumentException: 
No enum constant br.com.modelo.enumerados.Situacao.Bloqueada

Enum:


    public enum Situacao{   
         L("Livre"), B("Bloqueada");
        
        private final String nome;
        
        Situaca(String nome){
            this.nome = nome;
        }
    
        public String getNome() {
            return nome;
        }
        
        @Override
        public String toString(){
            return this.nome;
        }
        
        public static SituacaoPosicao getSituacao(String valor){
            for(Situacao st : Situacao.values()){
                if(st.nome.equals(valor)){
                    return st;
                }
            }
            throw new IllegalArgumentException();
        }
    
    }

Convert:

    @FacesConverter("situacaoConverter")
    public class SituacaoConverter implements Converter{
    
        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String value) {
                if (value != null) {
                    return Situacao.valueOf(value);
                }
    
                return null;
        }
    
        @Override
        public String getAsString(FacesContext context, UIComponent component, Object value) {
            if (value != null && value instanceof Situacao) {
                return ((Situacao) value).getNome();
            }
            return null;
        }
    
    }

Page XHTML:


   <h:form id="formularioAlteracaoCarro">
         
            <p:messages id="messages" showDetail="true">
                    <p:autoUpdate />
                </p:messages>
            
        <div class="ui-g ui-fluid">
            
            <div class="ui-g-12">
                
                <div class="card card-w-title">
                    <h1>Cadastrar carro</h1>
                    
                    <p:panelGrid columns="2" layout="grid" styleClass="ui-panelgrid-blank form-group" id="formDadosBox">
                        
                        <p:selectOneMenu id="cbboxBox" value="#{carroMng.carro.box}" 
                                         converter="#{boxConverter}" required="true" 
                                         requiredMessage="#{msg.boxObrigatorio}"
                                      immediate="true">
                            <f:selectItem itemLabel="#{msg.optionSelecioneBox}"/>
                            <f:selectItems value="#{carroMng.listaBoxes}" var="box"
                                        itemValue="#{box}" itemLabel="#{box}"/>
                         </p:selectOneMenu>
                        
                    </p:panelGrid>
                    
                    <p:panelGrid columns="2" layout="grid" styleClass="ui-panelgrid-blank form-group" id="formDadosSituacao">
                        <h:panelGroup>
                            <p:outputLabel for="radioSituacao" value="#{msg.labelSituacao}" />
                            <p:selectOneRadio layout="responsive" columns="2" id="radioSituacao" 
                                              value="#{carroMng.carro.carro.situacao}" 
                                            converter="#{situacaoConverter}" 
                                             required="true" 
                                            requiredMessage="#{msg.situacaoObrigatoria}">
                                <f:selectItems value="#{carroMng.listarSituacoesAlteracao}" var="carrosituacao"
                                               itemValue="#{carrosituacao}" itemLabel="#{carrosituacao.nome}"/>
                            </p:selectOneRadio>
                            
                        </h:panelGroup>
                    </p:panelGrid>
                    
                     <p:panelGrid columns="2" layout="grid" styleClass="ui-panelgrid-blank form-group">
                         <p:commandButton icon="ui-icon-save" 
                                        value="#{msg.botaoSalvar}" oncomplete="PF('confirmacaoConclusao').show()"
                                        process="@form" update=":formularioAlteracaoCarro:confirmacaoDialog"
                                        title="#{msg.salvar}"/>  
                         <p:commandButton icon=" ui-icon-closethick" action="#{carroMng.cancelar()}" 
                                     value="#{msg.botaoCancelar}" immediate="true"
                                     title="#{msg.cancelar}"/>
                        
                    </p:panelGrid>
                    
                </div>
                
            </div>
            
        </div>
            
            <p:confirmDialog header="#{msg.mensagemConcluir}" 
                             message="Deseja realmente concluir"
                             widgetVar="confirmacaoConclusao" id="confirmacaoDialog">
                <p:commandButton value="#{msg.textoSim}" oncomplete="PF('confirmacaoConclusao').hide();"
                                 action="#{carroMng.concluirAlteracaoLote()}" process="@form"
                        update=":formularioAlteracaoCarro:formDadosSituacao :formularioAlteracaoCarro:formDadosBox" id="yesButton" />
                <p:button value="#{msg.textoNao}" onclick="PF('confirmacaoConclusao').hide(); return false;" />
            </p:confirmDialog>
            
            
    </h:form>

1 answer

0

Rodrigo,

This is because br.com.modelo.enumerados.Situacao.Bloqueada does not exist. What exists is br.com.modelo.enumerados.Situacao.B.

In your logic, you should not manipulate the Enumeration value, but the Enumeration itself. Use only the value String of the Enumeration to Print.

You already have a method created for this, the getSituacao. Then you should use the method below instead of valueOf():

  @Override
  public Object getAsObject(FacesContext context, UIComponent component, String value) {
     if (value != null) {
         return Situacao.getSituacao(value);
     }
    
     return null;
  }
  • I did so by submitting the form: java.lang.Illegalargumentexception at br.com.modelo.enumerados.Situacaoposicao.getSituacaoPosicao(Situacao.java:32) at br.com.converter.SituacaoConverter.getAsObject(Situacaoconverter.java:19)

  • which is line 32?

  • throw new Illegalargumentexception(); in the Situation class

  • I just ran the code on my machine and it works. Your method has some compilation problems that I will arrange for you.

  • This means that the value is not correct. Change the line 32 by: throw new Illegalargumentexception("Value does not exist = " + value);

  • Includes the whole form for verification because the subter is also not recovering the data informed....

  • That’s another problem. Let’s try to check the Enum problem, which is part of your question. You have switched line 32 by: throw new Illegalargumentexception("Value does not exist = " + value); ?

  • gave the following error: Caused by: javax.el.Elexception: Can’t set Property 'situacao' on class 'br.com.modelo.Car' to value 'Blocked'.

  • Rodrigo, this is another exception instead of Illegalargumentexception. The previous problem has been solved, if yes, click Solved the question. And then you should evaluate why your code isn’t working. This is a new exception and not the one you asked.

  • I understood the error after you changed to your solution could not set the value B or L in Enum generating the error. The problem is setting this value that should be either B or L when submitting the form

Show 5 more comments

Browser other questions tagged

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