Show FacesMessage after redirection from servlet or JAX-RS using Flash

StackOverflow https://stackoverflow.com/questions/23395290

  •  12-07-2023
  •  | 
  •  

Domanda

I want to implement activation link via servlet or JAX-RS (not important which one), and later redirect user to proper page leaving him a FacesMessage stored in Flash. I have no problem with redirection itself, however I don't know how to access Flash.

È stato utile?

Soluzione

You can obtain access to the Flash object from the ExternalContext:

  1. Obtain access to the Flash:

    Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
    
  2. Set keepMessages =true on the object, to ensure that the FacesMessage you store in it survives the redirect

    flash.setKeepMessages(true);
    
  3. Store your FacesMessages using the putNow method, to ensure that even after the redirect, the messages will survive at least one page refresh

    flash.putNow(yourFacesMessage);
    

You really don't need to store the FacesMessage in the flash object itself; queuing them in the FacesContext is enough to guarantee that they'll be available on the other page

Related:

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top