javax.el.PropertyNotFoundException: itemLabel=“#{p

2020-05-08 14:09发布

问题:

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

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

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

Here is my code:

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>

The converter

@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"));
    }

}
}

And my Entity :

@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?

回答1:

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

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}" />

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.