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>
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)
– Rodrigo
which is line 32?
– Patrick Santana
throw new Illegalargumentexception(); in the Situation class
– Rodrigo
I just ran the code on my machine and it works. Your method has some compilation problems that I will arrange for you.
– Patrick Santana
This means that the value is not correct. Change the line 32 by: throw new Illegalargumentexception("Value does not exist = " + value);
– Patrick Santana
Includes the whole form for verification because the subter is also not recovering the data informed....
– Rodrigo
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); ?
– Patrick Santana
gave the following error: Caused by: javax.el.Elexception: Can’t set Property 'situacao' on class 'br.com.modelo.Car' to value 'Blocked'.
– Rodrigo
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.
– Patrick Santana
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
– Rodrigo