Primefaces p:全局实体转换器的自动完成

Primefaces p:全局实体转换器的自动完成,primefaces,autocomplete,converter,Primefaces,Autocomplete,Converter,我想构建一个p:autocomplete,用于从实体中搜索不同的对象。 像客户、文章 搜索工作正常,实体将出现。 但是转换器仍然不能工作 这是我的代码: <p:autoComplete id="searchStartpage" size="35" maxlength="200" queryDelay="0" maxResults="10" minQueryLength="1"

我想构建一个p:autocomplete,用于从实体中搜索不同的对象。 像客户、文章

搜索工作正常,实体将出现。 但是转换器仍然不能工作

这是我的代码:

<p:autoComplete id="searchStartpage" size="35" maxlength="200"
                                    queryDelay="0" maxResults="10" minQueryLength="1"
                                    value="#{searchGeneralRequestController.selectedObject}"
                                    completeMethod="#{searchGeneralRequestController.completeObject}"
                                    var="o" itemLabel="#{object.id}" itemValue="#{object}"
                                    converter="objectForSearchConverter" forceSelection="false"
                                    itemtipMyPosition="left center" cache="false"
                                    itemtipAtPosition="right center">

                                    <p:ajax event="itemSelect"
                                        listener="#{searchGeneralRequestController.handleSelect}" />

                                    <p:column rendered="#{o.getClass().getSimpleName() == 'Mandatory'}">  
                                        #{o.surname} #{o.name}
                                    </p:column>

                                    <p:column rendered="#{o.getClass().getSimpleName() == 'Article'}">  
                                        #{o.name}
                                    </p:column>

                                </p:autoComplete>
我所有的实体都有hash和string方法。 对象值始终为空


有人能帮忙吗,请

对于
自动完成
试试这个
通用转换器

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

}
import java.util.Map;
导入java.util.Map.Entry;
导入java.util.UUID;
导入java.util.WeakHashMap;
导入javax.faces.component.UIComponent;
导入javax.faces.context.FacesContext;
导入javax.faces.convert.Converter;
导入javax.faces.convert.FacesConverter;
@FacesConverter(value=“entityConverter”)
公共类EntityConverter实现转换器{
私有静态映射实体=new WeakHashMap();
@凌驾
公共字符串getAsString(FacesContext上下文、UIComponent组件、对象实体){
已同步(实体){
如果(!entities.containsKey(实体)){
字符串uuid=uuid.randomUUID().toString();
实体。put(实体,uuid);
返回uuid;
}否则{
返回实体。获取(实体);
}
}
}
@凌驾
公共对象getAsObject(FacesContext上下文、UIComponent组件、字符串uuid){
for(条目:entities.entrySet()){
if(entry.getValue().equals(uuid)){
return entry.getKey();
}
}
返回null;
}
}

对于
自动完成
请尝试此
通用转换器

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

}
import java.util.Map;
导入java.util.Map.Entry;
导入java.util.UUID;
导入java.util.WeakHashMap;
导入javax.faces.component.UIComponent;
导入javax.faces.context.FacesContext;
导入javax.faces.convert.Converter;
导入javax.faces.convert.FacesConverter;
@FacesConverter(value=“entityConverter”)
公共类EntityConverter实现转换器{
私有静态映射实体=new WeakHashMap();
@凌驾
公共字符串getAsString(FacesContext上下文、UIComponent组件、对象实体){
已同步(实体){
如果(!entities.containsKey(实体)){
字符串uuid=uuid.randomUUID().toString();
实体。put(实体,uuid);
返回uuid;
}否则{
返回实体。获取(实体);
}
}
}
@凌驾
公共对象getAsObject(FacesContext上下文、UIComponent组件、字符串uuid){
for(条目:entities.entrySet()){
if(entry.getValue().equals(uuid)){
return entry.getKey();
}
}
返回null;
}
}

这里是我的ManagedBean代码:

@Override
    public Object getRowData(String rowKey) {
        List<Object> countries = (List<Object>) getWrappedData();

        for (Object searchGeneral : countries) {

            if (searchGeneral instanceof Mandatory) {
                Mandatory mandatory = (Mandatory) searchGeneral;
                if (mandatory.getId().equals(rowKey))
                    return searchGeneral;
            }

            if (searchGeneral instanceof Article) {
                Article article = (Article) searchGeneral;
                if (article.getId().equals(rowKey))
                    return searchGeneral;
            }

            if (searchGeneral instanceof Customer) {
                Customer customer = (Customer) searchGeneral;
                if (customer.getId().equals(rowKey))
                    return searchGeneral;
            }
        }
        return null;
    }

    @Override
    public Object getRowKey(Object searchGeneral) {

        if (searchGeneral instanceof Customer) {
            Mandatory mandatory = (Mandatory) searchGeneral;
            return mandatory.getId();
        }

        if (searchGeneral instanceof Customer) {
            Customer customer = (Customer) searchGeneral;
            return customer.getId();
        }

        return null;
    }

    public List<Object> completeObject(String query)
            throws SearchGeneralNotFoundException, UserNotFoundException {
        List<Object> suggestions = new ArrayList<Object>();

        query = query.trim();

        filteredSearchGeneral = searchGeneralService.searchForStartPage(query);

        for (Object p : filteredSearchGeneral) {

            if (p instanceof Article) {
                Article article = (Article) p;
                if (article.getName().toLowerCase()
                        .startsWith(query.toLowerCase()))
                    suggestions.add(p);
            }

            if (p instanceof Customer) {
                Customer customer = (Customer) p;
                if (customer.getName().toLowerCase()
                        .startsWith(query.toLowerCase()))
                    suggestions.add(p);
            }

            if (p instanceof Mandatory) {
                Mandatory mandatory = (Mandatory) p;
                if (mandatory.getSurname().toLowerCase()
                        .startsWith(query.toLowerCase()))
                    suggestions.add(p);
                if (mandatory.getName().toLowerCase()
                        .startsWith(query.toLowerCase()))
                    suggestions.add(p);
            }

            if (p instanceof User) {
                User user = (User) p;
                if (user.getName().startsWith(query))
                    suggestions.add(p);
            }

        }

        return suggestions;
    }

    public void handleSelect(SelectEvent event) {
        Object object = (Object) event;
        LOGGER.info("handleSelect: " + object.getClass().getName());

        if (event.getClass().getSimpleName() == "Article") {
            LOGGER.info("ARTICLE");
        }

        redirectToCorrectPage(object);
    }

    public String redirectToCorrectPage(Object object) {
        LOGGER.info("redirectToCorrectPage");

        String url = "";
        FacesContext ctx = FacesContext.getCurrentInstance();

        try {
            if (selectedObject instanceof Mandatory) {
                LOGGER.info("redirectToCorrectPage Mandatory");
                ExternalContext extContext = ctx.getExternalContext();
                url = extContext
                        .encodeActionURL(ctx
                                .getApplication()
                                .getViewHandler()
                                .getActionURL(ctx,
                                        "/portal/mandatoryRegionEdit.xhtml"));
                extContext.redirect(url);
            }
        }

        catch (IOException ioe) {
            throw new FacesException(ioe);
        }

        return url;
    }
@覆盖
公共对象getRowData(字符串rowKey){
列表国家=(列表)getWrappedData();
适用于(一般对象:国家){
if(searchGeneral instanceof必填){
强制强制=(强制)搜索一般;
if(必需的.getId().equals(rowKey))
返回搜索一般;
}
if(搜索文章的一般实例){
第条=(第条)总则;
if(article.getId().equals(rowKey))
返回搜索一般;
}
if(搜索客户的一般实例){
客户=(客户)搜索一般;
if(customer.getId().equals(rowKey))
返回搜索一般;
}
}
返回null;
}
@凌驾
公共对象getRowKey(对象搜索常规){
if(搜索客户的一般实例){
强制强制=(强制)搜索一般;
返回mandatory.getId();
}
if(搜索客户的一般实例){
客户=(客户)搜索一般;
返回customer.getId();
}
返回null;
}
公共列表completeObject(字符串查询)
抛出SearchGeneralNotFoundException、UserNotFoundException{
列表建议=新建ArrayList();
query=query.trim();
filteredSearchGeneral=searchGeneralService.searchForStartPage(查询);
用于(对象p:filteredSearchGeneral){
if(物品的p实例){
第条=(第条)p;
if(article.getName().toLowerCase())
.startsWith(query.toLowerCase())
建议.增加(p);
}
if(客户的p实例){
客户=(客户)p;
if(customer.getName().toLowerCase())
.startsWith(query.toLowerCase())
建议.增加(p);
}
if(p instanceof必填){
强制性=(强制性)p;
if(必选的.getNames().toLowerCase())
.startsWith(query.toLowerCase())
建议.增加(p);
if(必选的.getName().toLowerCase())
.startsWith(query.toLowerCase())
建议.增加(p);
}
if(用户的p实例){
用户=(用户)p;
if(user.getName().startsWith(查询))
建议.增加(p);
}
}
返回建议;
}
public void handleSelect(SelectEvent事件){
对象=(对象)事件;
LOGGER.info(“handleSelect:+object.getClass().getName());
if(event.getClass().getSimpleName()=“Article”){
LOGGER.info(“文章”);
}
重定向到更正页面(对象);
}
公共字符串重定向到更正页(对象){
L