Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 509
Holysh Avatar asked Apr 04 '13 11:04

Holysh


1 Answers

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!"));
like image 53
user1643352 Avatar answered Nov 08 '22 06:11

user1643352