我正在尝试在我的项目中使用primefaces autocomplete组件,以避免将特定转换器编写到我尝试使用omnifaces和here中建议的每个列表对象通过'BalusC',我试图以完全相同的方式复制,但无法完成这项工作。如果我在这里遗漏了什么,请查看下面的代码片段和建议吗?
代码[f:selectOneMenu]的第一部分工作正常,没有任何问题
<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>
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;
}
}
但我收到了以下错误...
提前致谢..
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]
答案 0 :(得分:5)
抱歉,我忽略了一行ListConverter文档。我错过了将ListConverter与原始列表一起提供,如下所述。
<o:converter converterId="omnifaces.ListConverter" list="#{mybean.lstStatus}" />
我通过添加上面一行代码来解决这个问题。
最终的xhtml将如下所示..
<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>
感谢omnifaces团队提供这样一个有用的实用工具......
答案 1 :(得分:0)
我遇到了同样的问题。我无法使用omnifaces.ListConverter
进行自动填充.BalCus可以回答这个问题。
但是对于auto-complete
,请尝试以下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;
}
}