对于bean,h:selectOneMenu / f:selectItems返回的值始终为null

时间:2011-12-21 09:57:22

标签: jsf-2 cdi selectonemenu

我还在尝试使用jsf2,ejb3.1,cdi和glassfish编写应用程序。 我有一个有selectOneMenu

的表单
<h:form prependId="false">
...
<f:validateBean>
...
<h:selectOneMenu value="#{bottleManagedBean.selectProducer}" id="selectproducerb"
                                             validatorMessage="#{messages.bottleaddinvalideproducer}"  immediate="true">
                                <f:converter binding="#{producerConverter}"/>
                                <f:selectItems value="#{bottleManagedBean.producerItems}" />
                            </h:selectOneMenu>
...

selectItems组件的显示很好,但是当我提交表单时,bottleManagedBean.selectProducer的值始终为null。

我的formbean

@Named("bottleManagedBean")
@RequestScoped
public class BottleManagedBean {
....

private List<Producer> producers;

    public List<Producer> getProducers() {
        if (producers == null) {
            setProducers(producerService.list(Producer.class));
        }
        return producers;
    }

    public void setProducers(List<Producer> producers) {
        this.producers = producers;
    }

    private Producer selectProducer;

    public Producer getSelectProducer() {
        return selectProducer;
    }

  private List<SelectItem> producerItems;

    public List<SelectItem> getProducerItems() {
        if (producerItems == null) {
            producerItems = new ArrayList<SelectItem>();
            for (Producer current : getProducers()) {
                producerItems.add(new SelectItem(current.getId(), current.getName()));
            }
        }
        return producerItems;
    }

    public void setProducerItems(List<SelectItem> producerItems) {
        this.producerItems = producerItems;
    }
...

转换器

@Named("producerConverter")
public class ProducerConverter implements Converter {

    @Inject
    BusinessService<Producer> service;

    private static Logger trace = Logger.getLogger(ProducerConverter.class.getCanonicalName());

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String id) {
        try {
            return service.findByID(Producer.class, Integer.parseInt(id));
        } catch (NumberFormatException e) {
            e.printStackTrace();
            throw new ConverterException(e);
        }
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object o) {
        String asString = null;
        if (o != null) {
            asString = String.valueOf(o);
        }
        return asString;
    }
}

我尝试调试应用程序。当我提交表单时,应用程序永远不会进入设置器。

我还尝试添加一个valuechangelistener,我的应用程序从未调用过此方法。

最后,我尝试提交一个整数(例如我的bean的id),当我提交时,该值已经很好地填充。

那么,......出了什么问题?

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

这是你应该如何在jsf2

中正确使用valuechangelistener

<f:ajax listener="#{bottleManagedBean.selectProducer}" />添加到您的<h:selectOneMenu

像这样:

<h:selectOneMenu value="#{bottleManagedBean.selectProducerValueChange}" id="selectproducerb"
                                         validatorMessage="#{messages.bottleaddinvalideproducer}">
                            <f:ajax listener="#{bottleManagedBean.selectProducerValueChange}" />
                            <f:converter binding="#{producerConverter}"/>
                            <f:selectItems value="#{bottleManagedBean.producerItems}" />
                        </h:selectOneMenu>

并在bean中实现selectProducerValueChange ...