我试图从h:selectOneMenu获取Object“Plage”,这就是为什么我创建了一个Converter,它使用dao(hibernate)将String id转换为数据库中存在的Plage对象 这是我的xhtml代码:
<h:selectOneMenu id="pays" styleClass="form-control selectpicker" value="#{service.plage}"
converter="Convert" >
<f:selectItems value="#{moyen.plages_l}" var="pl"
itemValue="#{pl}" itemLabel="plage entre #{pl.min} et #{pl.max}" />
</h:selectOneMenu>
这里是Converter的代码:
@FacesConverter(value="Convert")
public class PlageConverter implements Converter {
@Autowired
PlageBo plageBo;
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
// TODO Auto-generated method stub
int id = Integer.valueOf(value);
Plage pl = plageBo.getPlagebyid(id);
return pl;
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1,
Object service) {
// TODO Auto-generated method stub
Plage plage_conv = (Plage) service;
String idAsString = String.valueOf(plage_conv.getId_plage());
return idAsString;
}
public PlageBo getPlageBo() {
return plageBo;
}
public void setPlagebo(PlageBo plageBo) {
this.plageBo = plageBo;
}}
我在Plage类中覆盖了equals()和haschode(): 公共类Plage实现Serializable {
private int id_plage;
private Double min;
private Double max;
private MoyenPaiement_pays moyenPaiement_pays;
//Getters & Setters
@Override
public boolean equals(Object o) {
// TODO Auto-generated method stub
//return super.equals(arg0);
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Plage that = (Plage) o;
if ( id_plage!=that.id_plage) return false;
if (max != null ? !max.equals(that.max) : that.max != null) return false;
if (min != null ? !min.equals(that.min) : that.min != null) return false;
if (!moyenPaiement_pays.equals(that.moyenPaiement_pays)) return false;
return true;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
int result = max.hashCode();
result = 31 * result + (min != null ? min.hashCode() : 0);
return result;
}
当我尝试在Bean中获取变量plage时 我收到了这个错误:
1100: JSF1073 : javax.faces.FacesException intercepté durant le traitement de PROCESS_VALIDATIONS 3 : UIComponent-ClientId=, Message=null
java.util.logging.ErrorManager: 5
java.lang.NullPointerException
at java.util.PropertyResourceBundle.handleGetObject(Unknown Source)
at java.util.ResourceBundle.getObject(Unknown Source)
at java.util.ResourceBundle.getString(Unknown Source)
at java.util.logging.Formatter.formatMessage(Unknown Source)