2

I'm trying to send the value of an attribute of a managedbean called Categoria_foro from another managedbean called ControllerManagedForo, in which it is redirected to a view foro.xhtml, so that when it calls EL, Categoria_foro already has the values ​​set. The problem is that the value is not being set

I am using JSF 2.2.9 and primefaces 6.2, this is the code

@ManagedBean(name = "controllerManagedForo")
@ViewScoped
public class ControllerManagedForo {

    private String categoria_nombre;

    public void buscar_foro_porCategoria() {
        categoria_nombre = "something"
        ExternalContext ec = 
        FacesContext.getCurrentInstance().getExternalContext();
                ec.getRequestMap().put("categoria_nombre", 
        categoria_nombre);
        ec.redirect("foro.xhtml");
    }
}    

@ManagedBean(name = "categoria_foro")
@ViewScoped
public class Categoria_foro {

    private String nombre;

    @PostConstruct
    public void init() {
        ExternalContext ec = 
        FacesContext.getCurrentInstance().getExternalContext();
        nombre =  (String) ec.getRequestMap().get("categoria_nombre");

    } 
}

Obviously getters and setters are defined. The attribute nombre of Categoria_foro that receives the value of the key is null, any idea of ​​what the problem might be?

david_A
  • 23
  • 4
  • The reason it is null is meantioned in the comments on the answer of https://stackoverflow.com/questions/6377798/what-can-fmetadata-fviewparam-and-fviewaction-be-used-for – Kukeltje Apr 07 '19 at 20:49
  • When are you calling buscar_foro_porCategoria() and can you guarantee it is before the categoria_foro bean is created? – Michael Apr 09 '19 at 12:33
  • @Kukeltje Do you mean specifically to this comment?:"The PostConstruct of a view scoped bean is not invoked on every request" – david_A Apr 09 '19 at 18:15
  • No, the second comment below the answer. – Kukeltje Apr 09 '19 at 18:25
  • @Kukeltje ok, I understand that the life cycle of the viewscoped is interrupted because redirect generates a new request, but why the ExternalContext environment can not save the values ​​sent in getRequestMap? according to the documentation on ExternalContext: this class allows the Faces API to be unaware of the nature of its containing application environment. Generally speaking and asking a more comprehensive question, in what way should the attributes of a viewscoped bean be set before directing the view in which it lives? Thanks – david_A Apr 10 '19 at 01:05
  • @Michael buscar_foro_porCategoria() is called from a view, according to the category selected in a submenu. Within this method and before instantiating categoria_foro, the data of categoria_foro is obtained from the database and sent to getRequestMap, then request is generated to foro.xhtml, where the viewscoped bean categoria_foro is instantiated and has the null attributes, which is precisely what I do not expect, thanks – david_A Apr 10 '19 at 01:30
  • I finally found the solution in this answer https://stackoverflow.com/questions/16817395 – david_A Apr 12 '19 at 01:20

0 Answers0