Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 2 primefaces使用pojo自动完成_Jsf 2_Autocomplete_Primefaces_Pojo - Fatal编程技术网

Jsf 2 primefaces使用pojo自动完成

Jsf 2 primefaces使用pojo自动完成,jsf-2,autocomplete,primefaces,pojo,Jsf 2,Autocomplete,Primefaces,Pojo,我读了很多关于同一个组件的QA,但我觉得我遗漏了一些东西,因为我落后了一步。 在使用primefaces自动完成组件时,我甚至无法打开页面。 它的代码段是: <p:autoComplete value="#{indirizzoCtrl.selectedCodiceNazione}" completeMethod="#{indirizzoCtrl.completeNazione}" var="nazione" itemLabel="#{

我读了很多关于同一个组件的QA,但我觉得我遗漏了一些东西,因为我落后了一步。 在使用primefaces自动完成组件时,我甚至无法打开页面。 它的代码段是:

<p:autoComplete value="#{indirizzoCtrl.selectedCodiceNazione}"  
            completeMethod="#{indirizzoCtrl.completeNazione}"  
            var="nazione" itemLabel="#{nazione.nome}"   
            itemValue="#{nazione.codiceNazione}" />
为什么会这样?我真的搞不懂。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());  
        } 
    }

}
公共类转换器实现转换器{
最终静态记录器日志=Logger.getLogger(NazioneConverter.class);
@凌驾
公共对象getAsObject(FacesContext上下文、UIComponent组件、字符串值){
如果(value.trim()等于(“”){
返回null;
}否则{
试试{
IndirizzoRepository ir=新的IndirizzoRepository();
List nazioni=ir.getNazioneByName(值);
如果(nazioni.size()==1)返回nazioni.get(0);
else抛出新异常();
}捕获(例外e){
String msg=“Errore di conversione”;
日志错误(msg,e);
抛出新的ConverterException(新的FacesMessage(FacesMessage.SEVERITY_ERROR,msg,“Nonèuna nazione conosciuta”);
}  
}          
}
@凌驾
公共字符串getAsString(FacesContext上下文、UIComponent、对象值){
如果(value==null | | value.equals(“”){
返回“”;
}否则{
返回字符串.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 showcase中的itemValue=“#{nazione}”,但问题变成了映射到
nazione.nome
ItemLabel
属性。
我做错了什么?

你读过用户指南了吗

我必须说,我从未在pojo中使用过自动完成功能,但从我在《用户指南》中读到的内容来看,切阿泰·切维奇在那里说:

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

您可以发现,即使
player.name
和其他道具是字符串,也会实现转换器(
PlayerConverter


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

在自动完成中将
itemValue
itemValue=“{nazione.codiceNazione}”更改为
itemValue=“{nazione}”
converter=“{nazioneConverter}”
autocomplete

中的
converter=“nazioneConverter”
这对我很有效:
//转换器
@面转换器(value=“MarcaConverter”)
公共类MarcaConverter实现转换器{
马卡多马卡多;
公共对象getAsObject(FacesContext上下文、UIComponent组件、字符串值){
if(value==null | | value.equals(“”)
返回null;
试一试{
int id=Integer.parseInt(值);
返回marcaDAO.findMarcaById(id);
}捕获(例外e){
e、 printStackTrace();
抛出新的ConverterException(新的FacesMessage(FacesMessage.SEVERITY_ERROR,“Marca no válida”和“”);
}
}
公共字符串getAsString(FacesContext上下文、UIComponent、对象值){
if(value==null | | value.equals(“”)
返回null;
返回字符串.valueOf(((Marca)value.getCodigoMarca());
}
}
//--------------------------------------
//豆子
@ManagedBean
@视域
公共类MyBeans实现了可序列化{
二等兵马卡·马卡;
...
公共Marca getMarca(){
返回马卡;
}
塞特马尔卡公共空间(马尔卡m){
marca=m;
}
...
公共列表obtenerMarcasVehicleulos(字符串s){
List marcas,smarcas=newarraylist();
试一试{
marcas=marcaDAO.findAllMarcas();
如果(s.trim().equals(“”)返回marcas;
for(Marca m:marcas)
如果(m.getNombreMarca().toString()包含| | m.getNombreMarca().toLowerCase().contains(s.toLowerCase()){
smarcas.添加(m);
}
返回SMARCA;
}捕获(例外e){
//JsfUtil.showFacesMSSG(e,“错误信息”,FacesMessage.SEVERITY_WARN);
e、 printStackTrace();
JsfUtil.lanzar异常(e);
返回null;
}
}
//-----------------------------------------
//*.xhtml页面
...
...
//-----------------------------------------
//马卡级
公共类Marca实现了可序列化{
私有静态最终长serialVersionUID=1L;
私有整数codigoMarca;
私有字符串nombreMarca;
...

什么PF版本?其他PF组件在同一视图中正常工作吗?@BalusC:version 2.2,其他一切正常添加转换器后的错误消息是什么?@gurung:完全相同。如果我写入
itemValue=“#{nazione.codiceNazione}”
则错误消息完全相同。如果我将其更改为
itemValue=“#{nazione}”
然后抛出完全相同的错误,但引用了
itemLabel=“#{nazione.nome}”"
。在实践中,它无法从
nazione
对象中提取属性。@themarcuz有人解决了这个问题吗?我有完全相同的问题。我一直在研究这个问题,我认为我们不需要转换器,因为autoComplete的值字段是字符串,而不是Pojo。我非常希望得到一个答案!我阅读了用户的文章导游,但读了巴卢斯克的这篇文章,我明白我可以避免在我的情况下使用转换器……但也许我误解了什么。
<p:autoComplete value="#{indirizzoCtrl.indirizzo.nazione.codiceNazione}"  
            completeMethod="#{indirizzoCtrl.completeNazione}"  
            var="nazione" itemLabel="#{nazione.nome}" converter="#{nazioneConverter}"
            itemValue="#{nazione.codiceNazione}" forceSelection="true"  />
//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;
        ...