0
我想從選擇一個菜單中選擇自定義對象。它既不顯示錯誤也不顯示值。該怎麼辦?請幫幫我。提前致謝。選擇自定義對象選擇一個菜單JSF轉換器異常
現在我在的getAsObject看到空指針異常這一行:
return getCurrencyService().getCurrencyBtId(currencyId);
這是我的XHTML文檔
<h:panelGrid columns="2">
<p:outputLabel value="" />
<p:selectOneMenu id="CurrencyMenu" value="#{CurrencyMB.currency}" converter="currencyConverter" >
<f:selectItems value="#{CurrencyMB.currencyList}" var="currency" itemValue="#{currency}" itemLabel="#{currency.currencyName}" >
</f:selectItems>
<p:ajax update="currencyOut" />
</p:selectOneMenu>
<p:outputLabel value="Currency Id : #{CurrencyMB.currency.currencyId}" id="currencyOut" />
</h:panelGrid>
這是我managedBean類。
@ManagedBean(name = "CurrencyMB")
@RequestScoped
public class CurrencyManagedBean implements Serializable{
private Currency currency;
private List<Currency> currencyList;
public Currency getCurrency() {
return currency;
}
public void setCurrency(Currency currency) {
this.currency = currency;
}
public List<Currency> getCurrencyList() {
currencyList = new ArrayList<Currency>();
currencyList.addAll(getiCurrencyService().getCurrencies());
return currencyList;
}
public void setCurrencyList(List<Currency> currencyList) {
this.currencyList = currencyList;
}
}
這是我currencyConverter類代碼:
**@FacesConverter("currencyConverter")
public class CurrencyConverter implements Converter {
@ManagedProperty(value = "#{currencyService}")
private ICurrencyService currencyService;
public ICurrencyService getCurrencyService() {
return currencyService;
}
public void setCurrencyService(ICurrencyService currencyService) {
this.currencyService = currencyService;
}
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
int currencyId = Integer.valueOf(value);
return getCurrencyService().getCurrencyBtId(currencyId);
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
Currency currency = (Currency) value;
int currencyId = currency.getCurrencyId();
return String.valueOf(currencyId);
}
}**
它不以任何方式工作。 :( – Umair
)如果你對所寫的內容付出足夠的注意力,你將能夠得到答案,只需一步一步完成即可解決問題。 – skuntsel