primefaces自动完成选择返回null

时间:2013-05-09 11:57:55

标签: jsf jsf-2 autocomplete primefaces

自动完成工作正常,我的意思是当我写东西它从db和autocompletes获取值但是当我选择值时,它显示selectedParty为null。通过我使用这个转换器与selectOneMenu的方式,它工作正常,我可以很容易获得选定的值,但自动完成组件存在问题 我的观点;

<p:autoComplete id="partySearchAutoId" value="#{myController.selectedParty}"
                                    var="party" itemLabel="#{party.partyName}"
                                    itemValue="#{party}" converter="genericConverter"
                                    forceSelection="true"                                        
                                    completeMethod="#{myController.searchParty}">
                        <p:ajax event="itemSelect" update="soaInputTextId"/>
                    </p:autoComplete>

我的控制员;

    private Party selectedParty;

    public List<Party> searchParty(String query) {  

    List<Party> partyList = genericService.getByTemplate(new Party(), "partyName", query);


    return partyList;  
}

public Party getSelectedParty() {
    return selectedParty;
}

public void setSelectedParty(Party selectedParty) {
    this.selectedParty = selectedParty;
}

这是我的转换器;

@FacesConverter("genericConverter")
public class GenericConverter extends SelectItemsConverter {

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    return value.toString();
}  

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    return super.getAsObject(context, component, value); //To change body of generated  methods, choose Tools | Templates.
 }



}

1 个答案:

答案 0 :(得分:-2)

使用通用实体转换器

package com.sfp.web.beans;

import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.WeakHashMap;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value = "entityConverter")
public class EntityConverter implements Converter {

    private static Map<Object, String> entities = new WeakHashMap<Object, String>();

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object entity) {
        synchronized (entities) {
            if (!entities.containsKey(entity)) {
                String uuid = UUID.randomUUID().toString();
                entities.put(entity, uuid);
                return uuid;
            } else {
                return entities.get(entity);
            }
        }
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String uuid) {
        for (Entry<Object, String> entry : entities.entrySet()) {
            if (entry.getValue().equals(uuid)) {

                return entry.getKey();
            }
        }
        return null;
    }
}