Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserving JSF messages after a redirect

Tags:

jsf

jsf-2

myfaces

I have a JSF page (using MyFaces 2.0) that does a bit of data gathering the first time it is displayed. If it can't find some information, it is supposed to provide a message to that effect and redirect back to a different page. I've tried using the solution found here Preserving FacesMessage after redirect for presentation through <h:message> in JSF (setKeepMessages(true)) but the messages aren't displaying after the redirect. The only difference I can pick out is that I'm not using a navigation rule, I'm calling the redirect() call on the external context because this isn't occurring in a normal action.

Relevant code:

public void redirectToPageWithMessage(String inPage, String message, FacesMessage.Severity severity){
    getFlash().setKeepMessages(true);
    addMessage(message, severity);
    try {
        getFacesContext().getExternalContext().redirect(inPage);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Unfortunately this doesn't seem to be working. The redirect happens just fine, but the < messages /> tag isn't displaying the message. Is there something different about the way redirect() happens that prevents this from working?

like image 679
moneyt Avatar asked Sep 21 '11 16:09

moneyt


2 Answers

The code that saves the messages are executed after the phase ends (see Flash.doPostPhaseActions(FacesContext) ). So, it is expected it does not work, but maybe you can call Flash.doPostPhaseActions before the redirect. Note is not a "clean" solution, but it is possible.

like image 196
lu4242 Avatar answered Oct 23 '22 03:10

lu4242


I had the same problem and solve it not using ExternalContext.redirect() but to play with outcome for your actions.

That is to say, my action called by my buttons return a String (the outcome) which indicates the navigation rules to go to the next page. With that system, the messages are preserved.

like image 24
Anthony O. Avatar answered Oct 23 '22 03:10

Anthony O.