2013-12-17 41 views
1

使用下面的豆,我充满了国家的一种形式:注浆DAO给NullPointerException异常

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

转换器给一个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 Valu e“消息,而不是应该由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); 
    } 
} 
+0

我已经到转换器加到脸,配置样解释http://stackoverflow.com/questions/3630403/how-do-i-访问EJB-豆时,里面-A-自定义转换器。 –

回答

3

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

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

您引用您的转换器,像绑定属性: <f:converter binding="#{countryConverter}" />

+0

感谢它工作正常。 –