2

1 and JSF 2.0 + primefaces which is cool :D

And I have this validator, but I cannot get the persistence unit injected properly in it.

All other beans are working properlly, this is the only one that doesn't, it gives me a Nullpointer at the roleFacade

Is it illegal to inject in a validator ? is it too early in the life cycle ? what can it be ?

Thanks !

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;


@FacesConverter("rolesConverter")
@Stateless
public class RolesConverter implements Converter{

@EJB
private RoleFacade roleFacade;

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Roles role = null;
        if ((value != null) && (!value.equals(""))) {       
             role = roleFacade.find(Long.valueOf(value));       
        }
        return role;
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
        Integer retorno = null;
        if (!(value == null)) {
            Roles role = new Roles();
            role = (Roles) value;
            retorno = role.getId();
        }
        return retorno.toString();
}

}

And the Facade

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Stateless
public class RoleFacade extends AbstractFacade<Roles> {


@PersistenceContext(unitName = "br.com.cflex.itm-PU", type= PersistenceContextType.TRANSACTION)    
private EntityManager em;

private static Logger log = LoggerFactory.getLogger(RoleFacade.class);

@Override
protected EntityManager getEntityManager() {
    return em;
}

public RoleFacade() {
    super(Roles.class);
}

public RoleFacade(EntityManager em) {
    super(Roles.class);
    this.em = em;
}

public List<Roles> getListOfRoles(){
    log.debug("Calling method 'RoleFacade: List getListOfRoles()'");
    List<Roles> list = getEntityManager().createNamedQuery("Roles.findAll").getResultList();        
   return list;
}

}

Cristiano Fontes
  • 4,920
  • 6
  • 43
  • 76
  • primefaces a nightmare? why is that? – Cagatay Civici Oct 10 '11 at 21:18
  • Ajax issues with regular JSF ajax tag and some other very annoying bugs ? – Cristiano Fontes Oct 11 '11 at 13:21
  • The answer to my question can be found int this topic http://stackoverflow.com/questions/2019495/inject-a-ejb-into-a-jsf-converter-with-jee6 – Cristiano Fontes Oct 11 '11 at 13:42
  • 1
    Interesting, I have never heard of someone who thinks PF is nightmare, usually it is the other way. Well, we can't win always I guess. What are the annoying bugs, can you point me to issue tickets? Also what is the issue with regular JSF ajax tag? – Cagatay Civici Oct 11 '11 at 19:54
  • Just +1 you and removed my comment, because I was over reacting... My anger was related to JSF not primefaces, I like it and it's way better than the other alternatives. About the Ajax... if you try to use a the ajax is never called... you have to use the ajax inside p:commandButton. would be nice to be able to use JSF ajax tag. – Cristiano Fontes Oct 12 '11 at 14:17
  • And rowEditListener is gone from in PrimeFaces 3 – Cristiano Fontes Oct 12 '11 at 14:20
  • p:commandButton has built-in ajax so haven't added client behavior support, please add an enhancement request so we'll review. rowEditListener is p:ajax event="rowEdit" in 3.x. – Cagatay Civici Oct 12 '11 at 20:34
  • Ok nice... Will do it... thanks ! – Cristiano Fontes Oct 13 '11 at 14:03
  • cfontes, subscribe to PF blog: http://blog.primefaces.org/ and you will be aware of all new featurs of PF. "rowEdit" is the marvellous innovation of 3.0. Also don't understand, why you need f:ajax inside p:commandButton :). In my experience the only problems were with native f:ajax tags and others. Now I even don't use them. Only p: – bitec Oct 14 '11 at 06:14
  • I will do it ! thanks again ! – Cristiano Fontes Oct 14 '11 at 12:35

6 Answers6

5

You can do it, you're just doing it wrong.

Try this:

import java.io.Serializable;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.enterprise.context.RequestScoped
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Named;


@Named("rolesConverter")
@RequestScoped
public class RolesConverter implements Converter, Serializable {

@EJB
private RoleFacade roleFacade;

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Roles role = null;
        if ((value != null) && (!value.equals(""))) {       
             role = roleFacade.find(Long.valueOf(value));       
        }
        return role;
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
        Integer retorno = null;
        if (!(value == null)) {
            Roles role = new Roles();
            role = (Roles) value;
            retorno = role.getId();
        }
        return retorno.toString();
}

And in your xhtml, use converter="#{rolesConverter}". Of course, I'm assuming you're using CDI.

And PrimeFaces is awesome, by the way. :)

Steve
  • 8,066
  • 11
  • 70
  • 112
2

Another question you have to ask yourself is: Is it a correct design principle to have the converter to look in the database? My answer: definitely not. View is tied up with the model. Postpone that lookup of the real object until you do the changes to the DB (in persistence layer).

  • I don't like doing that either but for the pickList I need to be sure I got the real objects from the DB, how can It be done then ? – Cristiano Fontes Oct 12 '11 at 13:56
  • You only need real DB objects when you persist. So in your persistence layer, you can do a lookup of the real object, just before the persist, based on the id selected in the picklist on the screen. – Rudy De Busscher Oct 26 '11 at 13:34
1

Per the Java EE 6 platform specification, table EE.5-1, injection is only supported for JSF managed bean classes, not other JSF classes.

Brett Kail
  • 33,593
  • 2
  • 85
  • 90
1

Have you heard about CODI?

@Advanced annotation do the trick.

https://cwiki.apache.org/confluence/display/EXTCDI/JSF+Usage

0

Maybe you think this is off topic. However, as a web developer having used the majority (not all however) of frameworks, please stay far far far away from JSF. That must be the least productive most time-consuming hellish framework ever built. You want to make results? Learn Ext JS with REST. Maybe not suited for all apps but worthwhile for many.. Please consider this to be friendly advice without any intention to insult no one.

Lawrence
  • 1,035
  • 13
  • 8
0
  1. Keep @FaceConverter.
  2. Do a lookup in constructor.

    try {
        InitialContext ctx = new InitialContext();
        MyInterface myInstance = (MyInterface) ctx.lookup("java:module/MyEJB");
    } catch (NamingException e) {
        log.error(e.getMessage());
    }
    
Emmanuel
  • 131
  • 1
  • 4