How to bind the selected value in selectOneMenu
Of five options in a selectOneMenu I chose the second option and persisted the entity. On edit the persisted entity selectOneMenu always has the last option as its value.
For example,
<h:selectOneMenu value="#{userHome.user.leader}">
<f:selectItems value="#{userHome.availableLeaders}" var="leader" itemLabel="# {leader.name}" itemValue="#{leader}"/>
</h:selectOneMenu>
where availableLeaders is a list of users populated @PostConstruct method.
I am expecting the selectOneMenu to have the second option(chosen) on edit.
@FacesConverter(forClass = User.class, value = "userConverter")
public class UserConverter implements Converter {
public UserConverter() {
}
@Override
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("DefaultPersistenceUnit");
EntityManager em = emf.createEntityManager();
Query q = em.createQuery("select query");
return q.resultList().get(0);
}
@Override
public String getAsString(FacesContext context, UIComponent component,
Object value) {
return ((User) value).getName();
}}
In User.java
public boolean equals(Object other) {
if (this.getClass().isInstance(other)) {
return true;
} else {
return false;
}
}
public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder();
builder.append(getId());
开发者_如何学JAVAbuilder.append(getName());
return builder.toHashCode();
}
Look here:
public boolean equals(Object other) {
if (this.getClass().isInstance(other)) {
return true;
} else {
return false;
}
}
Your equals()
method is definitely broken. This returns true
for every other User
object, even though it internally holds a completely different user ID/name. So the selected item matches every available select item value. That's why you see the last item being preselected everytime.
Assuming that the id
property is unique for every user, then the equals()
method should at its simplest rather look like this:
public boolean equals(Object other) {
if (!(other instanceof User)) {
return false;
}
if (other == this) {
return true;
}
if (id != null) {
return id.equals(((User) other).id);
}
return false;
}
which can also be summarized as follows
public boolean equals(Object other) {
return (other instanceof User) && (id != null) ? id.equals(((User) other).id) : (other == this);
}
Hint: a bit decent IDE like Eclipse can autogenerate equals()
(and hashCode()
) method.
See also:
- Right way to implement equals contract
- Generic reflective helper method for equals and hashCode
精彩评论