【问题标题】:primefaces autocomplete with pojoprimefaces 使用 pojo 自动完成
【发布时间】:2012-01-23 12:13:00
【问题描述】:

我在 SO 上阅读了一些关于同一组件的 QA,但我觉得我遗漏了一些东西,因为我落后了一步。 在其中使用 primefaces 自动完成组件时,我什至无法打开页面。 它的sn-p是:

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

Nazione 是一个 Pojo 类,其中 CodiceNazioneNome are 两个字符串字段(肯定有 getter 和 setter)。 completeNazione 是 ManagedBean 上的一个方法,它返回 List&lt;Nazione&gt;。 查看 BalusC 的解释 here,在我看来,我不需要涉及任何转换器,因为 itemValue 和 value 属性都映射到字符串属性。 无论如何,当我打开包含此自动完成 sn-p 的页面时,它会因以下错误而崩溃:

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

为什么会这样?我真的无法得到它。 completeNazione 方法甚至还没有调用,所以它不应该知道任何Nazione。 它有什么问题?

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

    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());  
        } 
    }

}

现在视图中的组件如下所示:

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

但仍然无法正常工作。转换器甚至没有被调用:我在 faces-config.xml 文件中注册了它。 我还尝试了在 primefaces 展示中的 itemValue="#{nazione}" 但问题变成了ItemLabel 属性,映射到nazione.nome。 我做错了什么?

【问题讨论】:

  • 什么 PF 版本?其他 PF 组件在同一视图中是否正常工作?
  • @BalusC:2.2 版,其他一切正常
  • 添加转换器后的错误信息是什么?
  • @gurung:完全一样。如果我写itemValue="#{nazione.codiceNazione}",那么错误信息是完全一样的。如果我将其更改为itemValue="#{nazione}",则会引发完全相同的错误,但引用itemLabel="#{nazione.nome}"。实际上,它无法将属性从nazione 对象中拉出。
  • @themarcuz 有人发现这个问题吗?我有完全一样的问题。我一直在研究这个问题,我认为我们不需要转换器,因为 autoComplete 的 value 字段是一个字符串,而不是 POJO。非常感谢您的回答!

标签: jsf-2 autocomplete primefaces pojo


【解决方案1】:

这对我有用:

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

【讨论】:

    【解决方案2】:

    您是否阅读了用户指南? http://www.primefaces.org/documentation.html

    我必须说我从未使用 pojo 的自动完成功能,但根据我在用户指南中阅读的内容,Çağatay Çivici 说:

    请注意,使用 pojos 时,您需要插入自己的转换器。

    Here 你可以发现即使player.name 和其他道具都是字符串,也实现了转换器(PlayerConverter)。

    我承认这很有趣,我会做一些研究,但我现在没有必要的时间......

    【讨论】:

    • 我阅读了用户指南,但阅读了 BalusC stackoverflow.com/a/7653775/333223 的这篇文章,我知道我可以避免使用转换器......但也许我误解了一些东西......
    • @themarcuz 添加一个简单的转换器,你自己看看...没有练习你就学不会编程!
    • 我一定会做的...我现在正忙于另一项任务,但几个小时后我会尝试按照建议进行操作
    • @themarcuz 对你有好处!让我知道结果。我什至会尝试自己,但也许明天......
    • 添加了新信息...我尝试使用转换器,但没有任何变化:(
    【解决方案3】:

    改变 converter="#{nazioneConverter}"converter="nazioneConverter"autocomplete

    【讨论】:

      【解决方案4】:

      在自动完成中将 itemValueitemValue="#{nazione.codiceNazione}" 更改为 itemValue="#{nazione}"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-08
        • 2016-01-03
        • 2013-01-31
        • 1970-01-01
        • 2015-02-17
        • 2012-03-27
        • 2014-04-11
        • 1970-01-01
        相关资源
        最近更新 更多