Requesting help in converting to selectOneMenu JSF

Asked

Viewed 642 times

0

Look at the figure;

inserir a descrição da imagem aqui

When clicking save it generates this error message on screen, which indicates that it was not possible to use a null converter, saying that there is no null conversion to the check box shown in the figure, because the convert would convert the list into an object of type city, but there is a class that converts this list, and that even this same class is being used in another managerBean and is working perfect, just not working in this form, see below;

package br.com.terezinha.adm.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.terezinha.adm.model.Cidade;
import br.com.terezinha.adm.repository.Cidades;
import br.com.terezinha.adm.util.cdi.CDIServiceLocator;

@FacesConverter(value="estadoConverter", forClass = Cidade.class)
public class CidadeConverter implements Converter {

    private Cidades cidades;

    public CidadeConverter() {
        cidades = CDIServiceLocator.getBean(Cidades.class);
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Cidade retorno = null;
        if (value != null) {
            Long id = new Long(value);
            retorno = cidades.porId(id);
        }

        return retorno;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value != null) {
            Cidade cidade = (Cidade) value;
            return cidade.getCidadeId() == null ? null : cidade.getCidadeId().toString();


//          if (cidade.getCidadeId() != null) {
//              return cidade.getCidadeId().toString();
//          } else if (cidade.getCidadePai() != null) {
//                  return cidade.getCidadePai().toString();
//          }
//          else if(cidade.getSubCidades() !=null){
//              return cidade.getSubCidades().toString();
//          }
//          else{
//              return null;
//          }

        }

        return "";
    }

}

I can’t understand why this is happening, I’ve made several attempts at correction as you can see.

I’ve tried even this way;

<p:selectOneMenu id="cidades" value="#{cadastroLocalidadeBean.cidadePai}" converter="estadoConverter"  >
    <f:selectItem itemLabel="Selecione a Bairro" />
    <f:selectItems value="#{cadastroLocalidadeBean.subcidades}"
        var="subcidade" itemValue="#{subcidade}"
        itemLabel="#{subcidade.descCidade}" />
</p:selectOneMenu>

And no result.

Someone would have a suggestion?

Here’s my Github project;

https://github.com/wladimirbandeira/Terezinha.Adm

  • Your object has the toString() method defined?

  • your question was so technical that I did not understand, but I just updated my post and include the path of my project in Github, I believe this will be easier for me help.

  • The city object does not need a toString method set to work, only one class converts to solve it, but the class converts is not solving. now I don’t understand why the city object should have a toString method? could answer this question?

  • I asked if I had toString because when you put in itemValue it can send the return of toString() and not the object.

  • what would be your suggestion?

  • 1

    I think your convert is failing to search the city and is returning null. In no example of converting that I saw has the converter builder, probably it is not starting your cities.

  • OK, thank you very much.

Show 2 more comments
No answers

Browser other questions tagged

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