1

Using the following Bean, I fill a form with countries:

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

In the form I've to use a Converter:

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

The Converter give a NullPointerException seems because it's unable to inject 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);
    }
} 
}

In the console I see the "Converted Value" message but not the "CountryDAO find" that should be printed by createDAO.find method.

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

I tried the solution reported to Inject a EJB into a JSF converter with JEE6 (I don't know if I put the code in the correct location). I put it in the converter (and I obtain the 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);
    }
}
Community
  • 1
  • 1
Enrico Morelli
  • 185
  • 3
  • 16
  • I've to add the Converter to faces-config like explain in http://stackoverflow.com/questions/3630403/how-do-i-access-ejb-bean-when-inside-a-custom-converter. – Enrico Morelli Dec 17 '13 at 15:20

1 Answers1

3

I was having the same problem in the some other project, where I was using a converter for some primefaces component. I solved the problem the CDI way.

All you have to do is annotate your converter class with @Named (and inject the DAO class via @Inject (JEE6), and not with JEE5 - @EJB).

You reference your converter with binding attribute like: <f:converter binding="#{countryConverter}" />

britulin
  • 310
  • 2
  • 4