Preserving JSF messages after a redirect

moneyt picture moneyt · Sep 21, 2011 · Viewed 8.6k times · Source

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?

Answer

lu4242 picture lu4242 · Sep 21, 2011

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.