简体   繁体   中英

Set value to ManagedProperty

I'm trying to set a value to my ManagedProperty but I'm getting the null result when I try to print this. I'd like to set the Bean Class to use it in my query. I've been tryin' set String, Class, but all the times it returned a null value. Can anyone help me?

@ManagedBean
public class FilialBean extends BaseBean implements Serializable{

private Filial filial;
private List<Filial> filiais;

@ManagedProperty("#{entidadeService}")
private EntidadeService service;


@PostConstruct
public void init(){

    service.setFaces(Filial.class);
    filial = new Filial();
    filiais = (List<Filial>) (List) service.getbasesEntidades();

}

//GETTERS AND SETTERS

}
@ManagedBean(name="entidadeService", eager=true)
@ApplicationScoped
public class EntidadeService implements Serializable{

private List<EntidadeBase> basesEntidades;  
private Class faces;

@PostConstruct
public void init(){

    System.out.println(faces.getSimpleName());
    try{
      EntityManager manager = JPAUtil.getEntityManager();
      Query query = manager.createQuery("SELECT e FROM Filial e WHERE e.ativo = :ativo");
      query.setParameter("ativo", true);
      this.basesEntidades = query.getResultList();
    }
    catch(Exception e){

        e.printStackTrace();
    }
}


public List<EntidadeBase> getbasesEntidades() {
    return basesEntidades;
}

public Class getFaces() {
    return faces;
}

public void setFaces(Class faces) {
    this.faces = faces;
}
}

Have you check that @ManagedBean has same package in both classes?

I ran into same problem, a property with null value executing Post Construct method and this is the problem, one class had javax.annotation.ManagedBean (CDI) annotation and the other had javax.faces.bean.ManagedBean (JSF) annotation.

In my case I needed both classes with JSF annotations...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM