我不确定这是否可行......但我想使用autoComplete组件,其中value属性的类型为String,其中completeMethod返回一些重型对象的List。
我也需要使用forceSelection="false"
这是我认为应该起作用的(但不是):
<p:autoComplete id="it_demandeur"
value="#{utilisateurDemandeurCtrl.critereRechercheDemandeur}"
var="demandeurItem"
itemLabel="#{demandeurItem ne null ? demandeurItem.numeroOW.toString().concat(' - ').concat(demandeurItem.nom) : ''}"
itemValue="#{demandeurItem.nom}"
completeMethod="#{utilisateurDemandeurCtrl.completeDemandeur}"
minQueryLength="3"
cacheTimeout="10000">
<p:column>
#{demandeurItem.numeroOW} - #{demandeurItem.nom}
</p:column>
</p:autoComplete>
这是返回建议列表的方法:
@SuppressWarnings("unchecked")
public List<Demandeur> completeDemandeur(String query) {
StringBuilder jpql = new StringBuilder(128);
jpql.append("SELECT d");
jpql.append(" FROM Demandeur d");
jpql.append(" WHERE UPPER(d.nom) LIKE :query");
jpql.append(" OR d.numeroOW LIKE :query");
Query demandeurQuery = em.createQuery(jpql.toString());
demandeurQuery.setParameter("query", "%" + query.toUpperCase() + "%");
return (List<Demandeur>) demandeurQuery.getResultList();
}
如果用户选择了一个建议,它会将itemValue设置为所选建议的名称,但会显示来自Demandeur对象的2个值的连接字符串。
建议确实出现了,我可以选择它们,但不幸的是,我在提交页面时遇到了这个错误:
异常消息:/ page/utilisateur.xhtml在第27行和第50列itemLabel =“#{demandeurItem ne null?demandeurItem.numeroOW.toString()。concat(' - ')。concat(demandeurItem.nom):' '}“:在类型java.lang.String
上找不到属性'numeroOW'我的理解是autoComplete组件的var属性是建议的迭代器,所以在我的Demandeur类型的情况下,不是String。
任何帮助将不胜感激!
由于
我在Websphere 8.5.5.0上使用primefaces 3.5.11,JSF的MyFaces实现
编辑:
这是我尝试过的转换器
@FacesConverter(value = "demandeurUIConverter")
public class DemandeurConverter implements Serializable, Converter {
private static final long serialVersionUID = 1L;
@Override
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) throws ConverterException {
if (value == null || value.length() == 0) {
return null;
}
ConverterCtrl cc = EJB.lookup(ConverterCtrl.class);
Demandeur d = cc.getDemandeurFromCle(value);
if (d == null) {
d = new Demandeur();
d.setNom(value);
d.setNumeroOW(value);
}
return d;
}
@Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) throws ConverterException {
if (value == null) {
return "";
}
Demandeur demandeur = (Demandeur) value;
return demandeur.getNom();
}
}
答案 0 :(得分:0)
如果您为Demandeur
制作转换器,这将有效。
答案 1 :(得分:0)
实际上,问题似乎是这部分
itemValue="#{demandeurItem.nom}"
如果itemValue是项目的名称,则转换器将尝试从String转换而不是从对象转换。因此,下面的转换器方法将收到“value”= string,而不是您所期望的Demandeur。
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) throws ConverterException
我在下面尝试过这段代码,我认为这就是你需要的。我正在使用primefaces 4.0 onmee 1.6.0。
转换器
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
@ManagedBean
@RequestScoped
public class DemandeurConverter implements Converter, Serializable {
private static final long serialVersionUID = 1L;
@EJB
Demandeurs ejb;
@Override
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) throws ConverterException {
if (value == null || value.length() == 0) {
return null;
} else {
return ejb.getData().get(Long.parseLong(value));
}
}
@Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) throws ConverterException {
System.out.println(value.getClass());
if (value == null) {
return null;
} else {
return ((Demandeur) value).getId().toString();
}
}
}
托管bean
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class UtilisateurDemandeurCtrl implements Serializable {
private static final long serialVersionUID = -3027573774106311465L;
@EJB
private Demandeurs ejb;
private Demandeur critereRechercheDemandeur;
public List<Demandeur> completeDemandeur(String query) {
List<Demandeur> l = new ArrayList<Demandeur>();
for(Entry<Long, Demandeur> entryset:ejb.getData().entrySet()){
if (entryset.getValue().getNom().contains(query)){
l.add(entryset.getValue());
}
}
return l;
}
public Demandeur getCritereRechercheDemandeur() {
return critereRechercheDemandeur;
}
public void setCritereRechercheDemandeur(Demandeur critereRechercheDemandeur) {
this.critereRechercheDemandeur = critereRechercheDemandeur;
}
}
EJB
import java.util.HashMap;
import java.util.Map;
import javax.ejb.Singleton;
@Singleton
public class Demandeurs {
private static final Map<Long,Demandeur> data = new HashMap<Long,Demandeur>(){
private static final long serialVersionUID = -4394378761837292672L;
{
put(1L,new Demandeur(1L,"ooooooooooone",111));
put(2L,new Demandeur(2L,"ttttttttttttwo",222));
}
};
public static Map<Long, Demandeur> getData() {
return data;
}
}
实体bean
import java.io.Serializable;
public class Demandeur implements Serializable{
private static final long serialVersionUID = 4023658749746098762L;
private Long id;
private String nom;
private Integer numeroOW;
public Demandeur() {}
public Demandeur(Long id, String nom, Integer numeroOW) {
super();
this.id = id;
this.nom = nom;
this.numeroOW = numeroOW;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public Integer getNumeroOW() {
return numeroOW;
}
public void setNumeroOW(Integer numeroOW) {
this.numeroOW = numeroOW;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((nom == null) ? 0 : nom.hashCode());
result = prime * result + ((numeroOW == null) ? 0 : numeroOW.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Demandeur other = (Demandeur) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (nom == null) {
if (other.nom != null)
return false;
} else if (!nom.equals(other.nom))
return false;
if (numeroOW == null) {
if (other.numeroOW != null)
return false;
} else if (!numeroOW.equals(other.numeroOW))
return false;
return true;
}
}
和xhtml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test</title>
<h:outputScript library="js" name="common.js" />
</h:head>
<h:body>
<h:form id="somePrefix">
<p:autoComplete
id="it_demandeur"
value="#{utilisateurDemandeurCtrl.critereRechercheDemandeur}"
var="demandeurItem"
converter="#{demandeurConverter}"
itemLabel="#{demandeurItem ne null ? demandeurItem.numeroOW.toString().concat(' - ').concat(demandeurItem.nom) : ''}"
itemValue="#{demandeurItem}"
forceSelection="true"
completeMethod="#{utilisateurDemandeurCtrl.completeDemandeur}">
<p:column>
#{demandeurItem.numeroOW} - #{demandeurItem.nom}
</p:column>
</p:autoComplete>
</h:form>
</h:body>
</html>
答案 2 :(得分:0)
我遇到了同样的问题。
这给了我一个错误:itemLabel =&#34;#{user.fullName}&#34;:Property&#39; fullName&#39;在类型java.lang.String
上找不到只有在提交后更新自动完成组件(显示验证消息)时才会出现此问题。如果您删除&#34;更新&#34;来自commandButton的属性,它可以正常工作。
<p:panel id="resourceConfigNewFormPanel">
<p:autoComplete id="newUsername" value="#{userResourceConfigurationListBean.resourceConfig.username}" completeMethod="#{userResourceConfigurationListBean.autocompleteUser}" var="user" itemLabel="#{user.fullName}" itemValue="#{user.userName}" forceSelection="true" required="true">
</p:autoComplete>
<p:commandButton value="..." action="..." update="resourceConfigNewForm" oncomplete="if (args && !args.validationFailed) resourceConfigNewDlg.hide()" />
这可以使用和不使用更新:
<p:autoComplete id="newUsername" value="#{userResourceConfigurationListBean.resourceConfig.user}" completeMethod="#{userResourceConfigurationListBean.autocompleteUser}" var="user" itemLabel="#{user.fullName}" itemValue="#{user}" converter="#{userNameToUserConverter}" forceSelection="true" required="true">
</p:autoComplete>