javax.el.PropertyNotFoundException:itemLabel =“#{projet.nomProjet}":在类型java.lang.String上找不到属性'nomProjet' [英] javax.el.PropertyNotFoundException: itemLabel="#{projet.nomProjet}": Property 'nomProjet' not found on type java.lang.String

查看:120
本文介绍了javax.el.PropertyNotFoundException:itemLabel =“#{projet.nomProjet}":在类型java.lang.String上找不到属性'nomProjet'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JSF转换器应用于selectOneMenu内部的Entity, 但是无法识别该转换器,我在xhtml文件中得到此警告

I'm trying to apply a JSF converter to an Entity inside a selectOneMenu, but the converter is not recognized, I get this warning in my xhtml file,

<<"nomProjet" cannot be resolved>>

,当我运行该应用程序时,出现错误HTTP 500:

and when I run the application I'm getting Error HTTP 500 :

itemLabel="#{projet.nomProjet}": Property 'nomProjet' not found on type java.lang.String

这是我的代码:

我视图中的selectOneMenu

The selectOneMenu in my view

<p:selectOneMenu id="projet" converter="projetConverter" value="# {affectation.selectedProjet}" >
                                <f:selectItems var="projet" itemValue="#{projet}" itemLabel="#{projet.nomProjet}" value="#{affectation.projetsAffectablesCollaborateur()}" />
                            </p:selectOneMenu>

转换器

@Component
@FacesConverter("projetConverter")
public class ProjetConverter implements Converter {

@Autowired
private ProjetRepository projetRepository;

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    if (value == null || value.isEmpty()) {
        return null;
    }

    try {
        Projet projet = projetRepository.findByIdProjet(Long.valueOf(value));
        return projet;
    } catch (NumberFormatException exception) {
        throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erreur de conversion", "ID de projet invalide"));
    }

}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (value == null) {
        return "";
    }

    if (value instanceof Projet) {
        return String.valueOf(((Projet) value).getIdProjet());
    } else {
        throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erreur de conversion", "Instance de projet invalide"));
    }

}
}

我的实体:

@Entity
@NamedQuery(name = "Projet.findAll", query = "SELECT p FROM Projet p")
public class Projet implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long idProjet;

private String nomProjet;
@Transient
private List<Role> listRoles = new ArrayList<Role>();

public List<Role> getListRoles() {
    return listRoles;
}

public void setListRoles(List<Role> listRoles) {
    this.listRoles = listRoles;
}

// bi-directional many-to-one association to AffectationProjetRole
@OneToMany(mappedBy = "projet")
private List<AffectationProjetRole> affectationProjetRoles;

public Projet() {
}

public Projet(String nomProjet) {
    this.nomProjet = nomProjet;
}

public long getIdProjet() {
    return this.idProjet;
}

public void setIdProjet(long idProjet) {
    this.idProjet = idProjet;
}

public String getNomProjet() {
    return this.nomProjet;
}

public void setNomProjet(String nomProjet) {
    this.nomProjet = nomProjet;
}

public List<AffectationProjetRole> getAffectationProjetRoles() {
    return this.affectationProjetRoles;
}

public void setAffectationProjetRoles(List<AffectationProjetRole> affectationProjetRoles) {
    this.affectationProjetRoles = affectationProjetRoles;
}

public AffectationProjetRole addAffectationProjetRole(AffectationProjetRole affectationProjetRole) {
    getAffectationProjetRoles().add(affectationProjetRole);
    affectationProjetRole.setProjet(this);

    return affectationProjetRole;
}

public AffectationProjetRole removeAffectationProjetRole(AffectationProjetRole affectationProjetRole) {
    getAffectationProjetRoles().remove(affectationProjetRole);
    affectationProjetRole.setProjet(null);

    return affectationProjetRole;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + (int) (idProjet ^ (idProjet >>> 32));
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Projet other = (Projet) obj;
    if (idProjet != other.idProjet)
        return false;
    return true;
}
}

这是怎么引起的,我该如何解决?

How is this caused and how can I solve it?

推荐答案

itemLabel =#{projet.nomProjet}":在类型java.lang.String上找不到属性'nomProjet'

itemLabel="#{projet.nomProjet}": Property 'nomProjet' not found on type java.lang.String

此错误消息告诉#{projet}在运行时实际上是java.lang.String.让我们看看#{projet}的来源.

This error message tells that #{projet} is during runtime actually a java.lang.String. Let's look where's #{projet} is coming from.

<f:selectItems value="#{affectation.projetsAffectablesCollaborateur()}"
    var="projet" itemValue="#{projet}" itemLabel="#{projet.nomProjet}" />

因此,#{affectation.projetsAffectablesCollaborateur()}实际上返回了List<String>.如果这是意外情况,请提防泛型擦除,并仔细检查所有未经检查的强制类型转换,以确保不正确地假定了泛型.通常,错误在持久层中.例如,当您错误地执行查询SELECT p.nomProject FROM Project p而不是SELECT p FROM Project p,然后对List<Projet>而不是List<String>执行未经检查的强制转换.

Thus, #{affectation.projetsAffectablesCollaborateur()} actually returned a List<String>. If this is unexpected, then beware of generic type erasure and doublecheck all unchecked casts that the generic type is not incorrectly assumed. Generally, the mistake is in the persitence layer. For example, when you mistakenly execute the query SELECT p.nomProject FROM Project p instead of SELECT p FROM Project p and then performed an unchecked cast against List<Projet> instead of List<String>.

请注意,渲染项目标签根本不涉及转换器,因此没有必要显示和指责它.该转换器仅用于项目值.

Do note that rendering item labels doesn't involve the converter at all, so showing and blaming it is unnecessary. The converter is only used on item values.

这篇关于javax.el.PropertyNotFoundException:itemLabel =“#{projet.nomProjet}":在类型java.lang.String上找不到属性'nomProjet'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆