-1

自动完成工作正常,我的意思是当我写东西时,它会从 db 获取值并自动完成,但是当我选择值时,它显示 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.
 }



}
4

1 回答 1

-2

使用通用实体转换器

包 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;
    }
}
于 2013-05-09T12:34:16.603 回答