I am facing a problem to populate SelectOneMenu correctly using converter. I followed several google references and lastly http://balusc.blogspot.in/2007/09/objects-in-hselectonemenu.html link of BalusC. But could not figure out the problem. I am giving all my entity, managedbean and jsf code for your consideration.
Entity:
@Entity
@Table(name = "gender")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Gender.findAll", query = "SELECT g FROM Gender g"),
@NamedQuery(name = "Gender.findById", query = "SELECT g FROM Gender g WHERE g.id = :id"),
@NamedQuery(name = "Gender.findByGender", query = "SELECT g FROM Gender g WHERE g.gender = :gender")})
public class Gender implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "id")
private Short id;
@Size(max = 10)
@Column(name = "gender")
private String gender;
@OneToMany(mappedBy = "gender")
private List<Patient> patientList;
public Gender() {
}
public Gender(Short id) {
this.id = id;
}
public Short getId() {
return id;
}
public void setId(Short id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@XmlTransient
public List<Patient> getPatientList() {
return patientList;
}
public void setPatientList(List<Patient> patientList) {
this.patientList = patientList;
}
@Override
public int hashCode() {
return (id != null) ? (getClass().hashCode() + id.hashCode()) : super.hashCode();
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
return (object instanceof Gender) && (id != null) ? id.equals(((Gender) object).id) : (object == this);
}
@Override
public String toString() {
return "hms.models.Gender[ id=" + id + " ]";
}
Converter:
@FacesConverter(value = "genderConverter")
public class GenderConverter implements Converter {
@Inject GenderService gs;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if(value == null){
return null;
}
return gs.getGender(Integer.parseInt(value));
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if(value == null) {
return null;
}
if (!(value instanceof Gender)) {
throw new ConverterException("The value is not a valid Gender: " + value);
}
return ((Gender) value).getGender();
}
}
And JSF SelectOneMenu tag:
<h:selectOneMenu id="gender" value="#{registrationBean.patient.gender}" converter="genderConverter">
<f:selectItem itemValue="0" itemLabel="-- Select Gender --" />
<f:selectItems value="#{registrationBean.genderList}" />
</h:selectOneMenu>
The dropdown output is similar to "hms.models.Gender[id=1]
" and so on. Image sample has given below:
Can anybody help? Thanks in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…