【问题标题】:Injecting DAO give NullPointerException注入 DAO 给 NullPointerException
【发布时间】:2013-12-17 11:01:49
【问题描述】:

使用以下 Bean,我填写了国家/地区的表格:

@ManagedBean  
@RequestScoped  
public class CreateUser {  

    @EJB  
    private ParticipantDAO participantDAO;  
    @EJB  
    private CountryDAO countryDAO;  
    private List<Country> countries = new ArrayList<Country>();  
   . . .  
   . . .  
   . . .  

    @PostConstruct  
    public void init() {  
        countries = countryDAO.getAllCountries();  
    }  

在我必须使用转换器的形式中:

 <h:selectOneMenu id="country" value="#{createUser.user.country}" required="true" requiredMessage="Please select a country." converter="#{countryConverter}" >
        <f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
        <f:selectItems value="#{createUser.countries}" var="country" itemValue="#{country}" itemLabel="#{country.country}" />
 </h:selectOneMenu>

Converter 给出 NullPointerException 似乎是因为它无法注入 CountryDAO:

@ManagedBean
@RequestScoped
@FacesConverter(forClass = Country.class)
public class CountryConverter implements Converter {

@EJB
private CountryDAO countryDAO;

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (!(value instanceof Country)) {
        return null;
    }

    return String.valueOf(((Country) value).getId());
}

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    if (value == null || value.isEmpty()) {
        return null;
    }
    try {
        System.out.println("Converter Value: " + value);
        Country c = countryDAO.find(Long.valueOf(value));
        System.out.println("Converter: " + c.getCountry());
        return c;
    } catch (Exception e) {
        throw new ConverterException(new FacesMessage(String.format("Cannot convert %s to Country %s %d", value, e.toString(), Long.valueOf((value)))), e);
    }
} 
}

在控制台中,我看到了“Converted Value”消息,但没有看到应该由 createDAO.find 方法打印的“CountryDAO find”。

@Stateless
@LocalBean
public class CountryDAO {

public CountryDAO() {
}
@PersistenceContext
private EntityManager em;
@Resource
SessionContext context;

public List<Country> getAllCountries() {
    TypedQuery<Country> query = em.createNamedQuery(Country.FIND_ALL, Country.class);
    return query.getResultList();
}

public Country find(Long id) {
    System.out.println("CountryDAO find");
    Country c = em.find(Country.class, id);
    System.out.println(c.getCountry());
    return c;
}

我尝试了报告给Inject a EJB into a JSF converter with JEE6的解决方案(我不知道我是否将代码放在正确的位置)。我把它放在转换器中(并获得 NullPointerException):

@ManagedBean
@FacesConverter(forClass = Country.class)
public class CountryConverter implements Converter {

 //    @EJB
//    private CountryDAO countryDAO;
private InitialContext ic;
private CountryDAO countryDAO;

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (!(value instanceof Country)) {
        return null;
    }

    return String.valueOf(((Country) value).getId());
}

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    if (value == null || value.isEmpty()) {
        return null;
    }
    System.out.println("Converter Value: " + value);
    try {
        try {
            ic = new InitialContext();
            countryDAO = (CountryDAO) ic.lookup("java:global/DAO/CountryDAO");
        } catch (NamingException e) {
        }

        Country c = countryDAO.find(Long.valueOf(value));
        System.out.println("Converter: " + c.getCountry());
        return c;
    } catch (Exception e) {
        throw new ConverterException(new FacesMessage(String.format("Cannot convert %s to Country %s %d", value, e.toString(), Long.valueOf((value)))), e);
    }
}

【问题讨论】:

标签: jsf dependency-injection ejb converter


【解决方案1】:

我在其他一些项目中遇到了同样的问题,我在其中使用了一些 primefaces 组件的转换器。我用 CDI 的方式解决了这个问题。

您所要做的就是使用@Named 注释您的转换器类(并通过@Inject (JEE6) 注入DAO 类,而不是使用JEE5 - @EJB)。

您使用绑定属性引用您的转换器,例如: &lt;f:converter binding="#{countryConverter}" /&gt;

【讨论】:

    猜你喜欢
    • 2019-02-21
    • 2014-10-06
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多