Pick Custom Object from Select one menu JSF Converter Exception [duplicate]

空扰寡人 提交于 2019-12-11 15:44:26

问题


i want to pick custom object from select one menu. it neither shows an error nor values. what to do? please help me. thanks in advance.

Now i'm seeing Null pointer exception at getAsObject at this line:

return getCurrencyService().getCurrencyBtId(currencyId);

this is my xhtml document

<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>

this is my managedBean class.

@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;
    }

}

this is my currencyConverter class code:

**@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);
    }
}**

回答1:


As it can be seen in many questions, and their answers, here, @FacesConverter is not eligible for injection. In your case, you try to inject your service via @ManagedProperty, which yields NPE, as Luiggi told you in a comment to your initial question.

With the current JSF release, all you can do to inject services in your converters is to give up using @FacesConverter and switch to using standard @ManagedBean annotation, thus referring to your converter not by id, but by bean name, like in converter="#{currencyConverter}".



来源:https://stackoverflow.com/questions/15733862/pick-custom-object-from-select-one-menu-jsf-converter-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!