Question

I am trying to use primefaces autocomplete component in my project, in order to avoid writing specific converter to each list object I am trying to use omnifaces as suggested in here and here by 'BalusC', I tried to replicate exactly same way but could not able get this work. please review below code snippet and advice if I am missing anything here?

First part of the code [f:selectOneMenu] is working absolutely fine without anyissues

XHTML

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:f="http://xmlns.jcp.org/jsf/core">
<ui:composition template="WEB-INF/layout/layout.xhtml">
    <ui:define name="content">
        <h:form>
            <h:outputText id="selected_status"
                value="#{mybean.objLuStatus.strDescColVal}" />
            <!-- h:selectOneMenu Working fine -->
            <h:selectOneMenu id="iterator2" value="#{mybean.objLuStatus}"
                converter="omnifaces.SelectItemsConverter">
                <f:selectItem itemLabel="Choose item" noSelectionOption="true" />
                <f:selectItems value="#{mybean.lstStatus}" var="entity"
                    itemLabel="#{entity.strDescColVal}" itemValue="#{entity}" />
                <f:ajax render="selected_status" />
            </h:selectOneMenu>
            <!-- p:autoComplete is NOT Working -->
            <p:autoComplete id="iterator3" value="#{mybean.objLuStatus}"
                converter="omnifaces.ListConverter" dropdown="true" var="entity"
                itemLabel="#{entity.strDescColVal}"
                placeholder="----- Select One -----" itemValue="#{entity}"
                completeMethod="#{mybean.completeList}">
                <f:ajax render="selected_status" />
            </p:autoComplete>
            <h:commandButton value="Submit" action="#{mybean.post}" />
        </h:form>
    </ui:define>
</ui:composition>
</html>

Managed Bean

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.ntr.foundation.model.LuStatus;

@ManagedBean(name = "mybean")
@ViewScoped
public class MyManagedBean extends BaseManagedBean implements Serializable {

    List<LuStatus> lstStatus;
    LuStatus objLuStatus;

    @PostConstruct
    public void init() {
        lstStatus = new ArrayList<LuStatus>();
        lstStatus.add(new LuStatus("1", "Draft", "DraftLong"));
        lstStatus.add(new LuStatus("2", "Approved", "ApprovedLong"));
    }

    public List<LuStatus> getLstStatus() {
        return lstStatus;
    }

    public void setLstStatus(List<LuStatus> lstStatus) {
        this.lstStatus = lstStatus;
    }

    public void post() {
        System.out.println("returned value " + objLuStatus);
    }

    public LuStatus getObjLuStatus() {
        return objLuStatus;
    }

    public void setObjLuStatus(LuStatus objLuStatus) {
        this.objLuStatus = objLuStatus;
    }

    public List<LuStatus> completeList(String strQuery) {
        return lstStatus;
    }

}

But I am getting below error...

Thanks in advance..

Caused by: java.lang.NullPointerException
    at org.omnifaces.converter.ListConverter.getAsObject(ListConverter.java:39) [omnifaces-1.7.jar:1.7]
    at org.primefaces.component.autocomplete.AutoCompleteRenderer.getConvertedValue(AutoCompleteRenderer.java:600) [primefaces-4.0.jar:4.0]
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1046) [jsf-api-2.2.4.jar:2.2]
    at javax.faces.component.UIInput.validate(UIInput.java:976) [jsf-api-2.2.4.jar:2.2]
Était-ce utile?

La solution

Sorry I overlooked one line of ListConverter documentation.. I missed feeding the ListConverter with the original list like mentioned below.

<o:converter converterId="omnifaces.ListConverter" list="#{mybean.lstStatus}" />

I got this worked by adding above line of code.

Final xhtml is going to look like below..

    <p:autoComplete id="iterator3" value="#{mybean.objLuStatus}" forceSelection="true"
        dropdown="true" var="entity"
        itemLabel="#{entity.strDescColVal}"
        placeholder="----- Select One -----" itemValue="#{entity}"
        completeMethod="#{mybean.completeList}">
        <f:ajax render="selected_status" />
         <o:converter converterId="omnifaces.ListConverter" list="#{mybean.lstStatus}" />
    </p:autoComplete>

Thank you to omnifaces team for providing such a useful utility...

Autres conseils

I had the same problem. I could not use omnifaces.ListConverter for autocomplete.BalCus could answer that.

But for auto-complete, try the below generic converter.

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

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top