primefaces autocomplete with pojo

房东的猫 提交于 2019-12-05 20:08:37

This worked for me:

//Converter
@FacesConverter(value="MarcaConverter")
public class MarcaConverter implements Converter{
    MarcaDAO marcaDAO;
    public Object getAsObject(FacesContext contet, UIComponent component, String value) {
        if(value==null || value.equals(""))
            return null;
        try{
            int id = Integer.parseInt(value);
            return marcaDAO.findMarcaById(id);
        }catch (Exception e) {
            e.printStackTrace();
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Marca no válida", ""));
        }
    }

    public String getAsString(FacesContext contet, UIComponent component, Object value) {
        if(value==null || value.equals(""))
            return null;
        return String.valueOf(((Marca)value).getCodigoMarca());
    }
}




//--------------------------------------
//Bean
@ManagedBean
@ViewScoped
public class MyBeans implements Serializable{
    private Marca marca;
    ...
    public Marca getMarca(){
        return marca;
    }
    public void setMarca(Marca m){
        marca=m;
    }
    ...
    public List<Marca> obtenerMarcasVehiculos(String s) {
        List<Marca> marcas,smarcas=new ArrayList<Marca>();
        try{
            marcas= marcaDAO.findAllMarcas();
            if(s.trim().equals("")) return marcas;
            for(Marca m:marcas)
                if (m.getNombreMarca().toString().contains(s) || m.getNombreMarca().toLowerCase().contains(s.toLowerCase())) {
                    smarcas.add(m);
                }
            return smarcas;
        }catch(Exception e){
            //JsfUtil.showFacesMsg(e,"Error al obtener las marcas de veh&iacute;culos","",FacesMessage.SEVERITY_WARN);
            e.printStackTrace();
            JsfUtil.lanzarException(e);
            return null;
        }
    }


//-----------------------------------------
//*.xhtml page
...
    <p:autoComplete 
       id="cbxMarca" value="#{myBean.marca}" size="40"
       converter="MarcaConverter"
       completeMethod="#{myBean.obtenerMarcasVehiculos}"
       var="m" itemLabel="#{m.nombreMarca}" itemValue="#{m}"
       forceSelection="true" dropdown="true"
       required="true" scrollHeight="200">
    </p:autoComplete>
...

//-----------------------------------------
//Class Marca
public class Marca implements Serializable{
       private static final long serialVersionUID = 1L;

    private Integer codigoMarca;
    private String nombreMarca;
        ...
spauny

Have you read the user guide? http://www.primefaces.org/documentation.html

I must say I have never used the autocomplete with pojo but from what I've read in the user guide, Çağatay Çivici says there:

Note that when working with pojos, you need to plug-in your own converter.

Here you can find out that a converter (PlayerConverter) is implemented even if player.name and of the other props are Strings.

I admit this is interesting and I'll do some research but I don't have the necessary time right now...

Marlon

Change converter="#{nazioneConverter}" to converter="nazioneConverter" in autocomplete

Mustafa

Change the itemValue from itemValue="#{nazione.codiceNazione}" to itemValue="#{nazione}" in autoComplete.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!