本文介绍了primefaces自动完成与POJO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了关于SO同一组件一些QA,但我觉得我失去了一些东西,因为我是一步落后。
使用primefaces它自动完成组件时,我甚至不能使页面打开。
它的片段是:

 <电话号码:自动完成值=#{} indirizzoCtrl.selectedCodiceNazione
            completeMethod =#{} indirizzoCtrl.completeNazione
            VAR =nazioneitemLabel =#{} nazione.nome
            itemValue =#{} nazione.codiceNazione/>

Nazione是一个POJO类,其中 CodiceNazione 诺姆是两个String字段(getter和setter肯定) 。 completeNazione 是返回上ManagedBean的方法列表与LT; Nazione>
看着这里 BalusC解释,在我看来,我不需要任何涉及转换器,因为这两个itemValue和价值属性映射到字符串属性。
无论如何,当我刚刚打开包含这个自动完成内容的网页,它与此错误崩溃:

  javax.el.PropertyNotFoundException:/Cliente/Indirizzo.xhtml @ 23,56 itemValue =#{} nazione.codiceNazione:itemValue =#{} nazione.codiceNazione:住宅'codiceNazione在java.lang.String类型未找到

为什么发生这种情况?我真的无法得到它。该方法completeNazione甚至没有叫呢,所以它不应该知道任何 Nazione 呢。
这有什么错呢?

编辑:
继建议,我试图添加一个转换器,但我仍然得到同样的错误。
这里是我的转换器:

 公共类NazioneConverter实现转换器{    最终静态记录器记录= Logger.getLogger(NazioneConverter.class);    @覆盖
    公共对象的getAsObject(FacesContext的上下文,UIComponent组件,字符串值){
        如果(value.trim()。等于()){
            返回null;
        }其他{
            尝试{
                IndirizzoRepository IR =新IndirizzoRepository();
                清单< Nazione> Nazioni酒店= ir.getNazioneByName(值);
                如果(nazioni.size()== 1)返回nazioni.get(0);
                否则抛出新的异常();            }赶上(例外五){
                弦乐味精=Errore迪转换器类;
                log.error(味精,E);
                抛出新的ConverterException(新的FacesMessage(FacesMessage.SEVERITY_ERROR,味精,非戊UNA nazione conosciuta));
            }
        }
    }    @覆盖
    公共字符串符getAsString(FacesContext的上下文,UIComponent组件,对象的值){
        如果(价值== NULL || value.equals()){
            返回;
        }其他{
            返回将String.valueOf(((Nazione)值).getNome());
        }
    }}

现在在视图组件如下:

 <电话号码:自动完成值=#{} indirizzoCtrl.indirizzo.nazione.codiceNazione
            completeMethod =#{} indirizzoCtrl.completeNazione
            VAR =nazioneitemLabel =#{} nazione.nome转换器=#{} nazioneConverter
            itemValue =#{} nazione.codiceNazioneforceSelection =真/>

但仍然没有工作。该转换器甚至没有调用:我注册在我的faces-config.xml文件。
我也试过itemValue =#{} nazione作为primefaces展示,但问题成为了 ItemLabel 属性,映射到 nazione.nome
我在做什么错了?


解决方案

这为我工作:

  //转换器
@FacesConverter(值=MarcaConverter)
公共类MarcaConverter实现转换器{
    MarcaDAO marcaDAO;
    公共对象的getAsObject(FacesContext的contet,UIComponent组件,字符串值){
        如果(价值== NULL || value.equals())
            返回null;
        尝试{
            INT ID =的Integer.parseInt(值);
            返回marcaDAO.findMarcaById(ID);
        }赶上(例外五){
            e.printStackTrace();
            抛出新的ConverterException(新的FacesMessage(FacesMessage.SEVERITY_ERROR,马卡报没有válida,));
        }
    }    公共字符串符getAsString(FacesContext的contet,UIComponent组件,对象的值){
        如果(价值== NULL || value.equals())
            返回null;
        返回将String.valueOf(((马卡)值).getCodigoMarca());
    }
}
// --------------------------------------
//豆
@ManagedBean
@ViewScoped
公共类MyBeans实现Serializable {
    私人马卡报马卡报;
    ...
    公共马卡getMarca(){
        返回马尔卡;
    }
    公共无效setMarca(马卡米){
        马卡报=米;
    }
    ...
    公开名单<&马卡GT; obtenerMarcasVehiculos(String s)将{
        清单<&马卡GT; MARCAS,smarcas =新的ArrayList<&马卡GT;();
        尝试{
            MARCAS = marcaDAO.findAllMarcas();
            如果(s.trim()等于()。)返回MARCAS;
            对于(马卡玛:MARCAS)
                如果(m.getNombreMarca()。的toString()。含有(S)|| m.getNombreMarca()。与toLowerCase()。包括(s.toLowerCase())){
                    smarcas.add(米);
                }
            返回smarcas;
        }赶上(例外五){
            //JsfUtil.showFacesMsg(e,\"Error人obtener拉斯MARCAS德辆&放大器; iacute; culos,,FacesMessage.SEVERITY_WARN);
            e.printStackTrace();
            JsfUtil.lanzarException(E);
            返回null;
        }
    }
// -----------------------------------------
//*.xhtml页
...
    <电话号码:自动完成
       ID =cbxMarcaVALUE =#{} myBean.marca大小=40
       转换器=MarcaConverter
       completeMethod =#{} myBean.obtenerMarcasVehiculos
       VAR =MitemLabel =#{} m.nombreMarcaitemValue =#{m}这样
       forceSelection =真下拉菜单=真
       所需=真scrollHeight属性=200>
    < / P:自动完成>
...// -----------------------------------------
//类马卡报
公共类马卡实现Serializable {
       私有静态最后的serialVersionUID长1L =;    私人整数codigoMarca;
    私人字符串nombreMarca;
        ...

I read on SO some QA about the same component, but I feel I'm missing something, because I am one step behind.I can't even make the page open when using the primefaces autocomplete component in it.The snippet for it is:

<p:autoComplete value="#{indirizzoCtrl.selectedCodiceNazione}"
            completeMethod="#{indirizzoCtrl.completeNazione}"
            var="nazione" itemLabel="#{nazione.nome}"
            itemValue="#{nazione.codiceNazione}" />

Nazione is a Pojo class where CodiceNazione and Nome are two String field (with getter and setter for sure). completeNazione is a method on the ManagedBean that returns List<Nazione>.Looking at BalusC explanation here, it seems to me that i don't need any converter involved, because both itemValue and value attributes are mapped to string property.Anyway, when I just open the page containing this autocomplete snippet, it crashes with this error:

javax.el.PropertyNotFoundException: /Cliente/Indirizzo.xhtml @23,56 itemValue="#{nazione.codiceNazione}": itemValue="#{nazione.codiceNazione}": Property 'codiceNazione' not found on type java.lang.String

Why this is happening? I really can't get it. The method completeNazione hasn't even called yet, so it shouldn't know any Nazione yet.What's wrong with it?

Edited:Following the suggestion, I tried to add a converter, but I still get the same error.Here's my converter:

    public class NazioneConverter implements Converter {

    final static Logger log = Logger.getLogger(NazioneConverter.class);

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value.trim().equals("")) {
            return null;
        } else {
            try {
                IndirizzoRepository ir = new IndirizzoRepository();
                List<Nazione> nazioni = ir.getNazioneByName(value);
                if (nazioni.size()==1) return nazioni.get(0);
                else throw new Exception();

            } catch (Exception e) {
                String msg = "Errore di conversione";
                log.error(msg, e);
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, "Non è una nazione conosciuta"));
            }
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null || value.equals("")) {
            return "";
        } else {
            return String.valueOf(((Nazione) value).getNome());
        }
    }

}

now the component in the view looks like:

<p:autoComplete value="#{indirizzoCtrl.indirizzo.nazione.codiceNazione}"
            completeMethod="#{indirizzoCtrl.completeNazione}"
            var="nazione" itemLabel="#{nazione.nome}" converter="#{nazioneConverter}"
            itemValue="#{nazione.codiceNazione}" forceSelection="true"  />

But still don't working. The converter is not even invoked: I registered it in my faces-config.xml file.I also tried itemValue="#{nazione}" as in the primefaces showcase but the problem became the ItemLabel attribute, mapped to nazione.nome.What am I doing wrong?

解决方案

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;
        ...

这篇关于primefaces自动完成与POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 03:32