我想使用selectOneMenu
在hibernate实体中设置多对一关系。
<h:selectOneMenu value="#{taskBean.instance.currency}" class="form-control" id="currencyList" converter="#{currencyConverterBean}">
<f:selectItems value="#{currencyBean.currencyList}" var="currency" itemLabel="#{currency.name}"/>
</h:selectOneMenu>
其中#{taskBean.instance}
返回一个hibernate实体任务:
@Entity
@Table(name="task")
public class Task {
//...
public Currency currency;
@ManyToOne
@JoinColumn(name = "currencyID")
public Currency getCurrency() {
return currency;
}
//...
}
如果为selectOneMenu设置值#{taskBean.instance.currency.id}
而为itemValue设置#{currency.id}"
则不起作用。因此,我尝试使用转换器:
@ManagedBean(name="currencyConverterBean")
@FacesConverter(value = "currencyConverter")
@RequestScoped
public class CurrencyConverter implements Converter {
CurrencyBO currencyBO;
@Override
public Currency getAsObject(FacesContext ctx, UIComponent component, String value) {
return getCurrencyBO().getCurrencyById(new Integer(value));
}
@Override
public String getAsString(FacesContext fc, UIComponent uic, Object o) {
return String.valueOf(((Currency) o).getId());
}
public CurrencyBO getCurrencyBO() {
return currencyBO;
}
public void setCurrencyBO(CurrencyBO currencyBO) {
this.currencyBO = currencyBO;
}
}
它不起作用。它返回以下错误:
PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException at com.otv.converter.CurrencyConverter.getAsObject(CurrencyConverter.java:33)
我做错了什么?