Show success message and then redirect to another page after a timeout using PageFlow

Holysh picture Holysh · Apr 4, 2013 · Viewed 20.8k times · Source

How can I show a success message and then redirect the user to another page after a timeout of e.g. 5 seconds?

I need this for the login page after a successful login. I tried the following and I can see the warning message on login failure, but not the success message on login success. It shows immediately the target page.

public String check(){
      if (username.equals("test") && password.equals("test")) {
          FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "PrimeFaces rocks!")); 
            return "Success";
        }else{
          FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"Sample warn message", "Watch out for PrimeFaces!"));  
            return "Failure";
        }
    }

I'm using Seam's PageFlow for navigation.

I have a

<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />

on the login page.

Answer

user1643352 picture user1643352 · Apr 4, 2013

It is one of the utilities of Flash. Instead of

FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "PrimeFaces rocks!"));:

simply use this code

FacesContext facesContext = FacesContext.getCurrentInstance();
Flash flash = facesContext.getExternalContext().getFlash();
flash.setKeepMessages(true);
flash.setRedirect(true);
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "PrimeFaces rocks!"));