0

我有一个省列表,显示在<p:selectOneMenu>

托管 bean 中的 java 代码:

public List<SelectItem> gouvernorats() {
    List<Gouvernorat> all = emetteursEJB.findAllGouvernorat();
    List<SelectItem> items = new ArrayList<>();
    for (Gouvernorat g : all) {
       items.add(new SelectItem(g, g.getLibelleGouv()));
    }
    return items;
}

<p:selectOneMenu>我添加<p:ajax>

<p:selectOneMenu value="#{emetteurBean.selectedGouvernorat}" style="width:160px"  >  
    <f:selectItem itemLabel="Select One" itemValue="" />  
    <f:selectItems value="#{emetteurBean.gouvernorats()}" />  
    <p:ajax event="change" listener="#{emetteursBean.handelGouvChanged()"/>
</p:selectOneMenu>

在方法中的handelGouvChanged() selectedGouvernorat对象总是null

最近我添加了我偶然发现的转换NullPointerException

@FacesConverter(forClass = Gouvernorat.class)
public class EmetteursConverter implements Converter {

    @EJB
    private ReferentielDaoLocal refEJB;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String selectedValue) {
        System.out.println("Inside The Converter");
        System.out.println(selectedValue.length());
        if (selectedValue == null) {
            return null;
        } else {
            return refEJB.findGouvByCode(selectedValue.trim());
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return null;
        } else {
            return String.valueOf(((Gouvernorat) value).getIdGouvernorat());
        }
    }
}
4

1 回答 1

0

我找到了一个解决方案,我注入了 EJBInitilContext.lookup ()

@FacesConverter(forClass = Gouvernorat.class)
public class GouvernoratConverter implements Converter {

    private static final Logger LOG = Logger.getLogger(GouvernoratConverter.class.getName());

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String selectedValue) {

        if (selectedValue == null) {
            return null;
        } else {
            try {
                ReferentielDaoLocal myService = (ReferentielDaoLocal) new     
                InitialContext().lookup("java:global/ErpCCF/ErpCCF-ejb/ReferentielDaoImpl");
                return myService.findGouvByCode(selectedValue);
            } catch (NamingException ex) {
                LOG.log(Level.SEVERE, "Converter Gouvernorat Error", ex.getMessage());
                return null;
            }
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return null;
        } else {
            return String.valueOf(((Gouvernorat) value).getIdGouvernorat());
        }
    }
}
于 2013-09-23T08:23:58.383 回答