0

The convert doesn't work. This is the code abount my xhtml(For now I need to list only all values):

<p:selectCheckboxMenu id="autoriRicerca"
                        value="#{ricercaLibroBean.autoriRicerca}" 
                        converter="autoriConverter"
                        multiple="true" filter="true" filterMatchMode="startsWith"
                        panelStyle="width:250px">

This is my Converter:

@FacesConverter("autoriConverter")
public class AutoriCoverter implements Converter {
    @Inject
    private AutoriDAO userService;

    public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {

         if (submittedValue == null || submittedValue.isEmpty()) {
                return null;
            }

            try {
                return userService.getListaAutori();//return the lis about  authors
            } catch (NumberFormatException e) {
                throw new ConverterException(new FacesMessage(String.format("%s is not a valid User ID", submittedValue)), e);
            }

    }

    public String getAsString(FacesContext context, UIComponent component, Object object) {
        if (object == null) {
            return "";
        }
        if (object instanceof Autori) {
            Autori autore = (Autori) object;
            String nomeAutore = autore.getNomeAutore();
            return nomeAutore;
        } else {
            throw new ConverterException(new FacesMessage(object + " is not a valid car"));
        }
    } 
}

in my bean I have:

private List<Autori> autoriRicerca; // and get and set method

the class Autori has a (id,nameAuthor).

I don't know which is the problem. It seems like the code doesn't enter in the converter method. I have put some breackpoints and they doesn't execute in debug. Anyone an help me?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Postilla
  • 161
  • 1
  • 1
  • 8

1 Answers1

-1

In the public Object getAsObject of the FacesConverter you have to return a single Object given the submitted id, not the complete list.

In the public String getAsString you have to return the object id given the object.

For example

public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {

     if (submittedValue == null || submittedValue.isEmpty()) {
            return null;
        }

        try {
            return userService.getAutoreByID(Long.valueOf(submittedValue));//return the author by id
        } catch (NumberFormatException e) {
            throw new ConverterException(new FacesMessage(String.format("%s is not a valid User ID", submittedValue)), e);
        }

}

public String getAsString(FacesContext context, UIComponent component, Object object) {
    if (object == null) {
        return "";
    }
    if (object instanceof Autori) {
        Autori autore = (Autori) object;
        Long autoreID = autore.getID();
        return autoreID;
    } else {
        throw new ConverterException(new FacesMessage(object + " is not a valid user"));
    }
}
Alexcat
  • 87
  • 4
  • 1
    You're not answering the real problem at all. *"Converter method getAsObject() getAsString() never called"*, *"it seems like the code doesn't enter in the converter method"*, *"I have put some breackpoints and they doesn't execute in debug"*. – BalusC Aug 27 '19 at 10:24