6

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?

Community
  • 1
  • 1
moneyt
  • 452
  • 8
  • 18

3 Answers3

5

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.

lu4242
  • 2,318
  • 1
  • 15
  • 15
  • Is there a better way (other than calling redirect()) to redirect inside a function that won't be returning a result? – moneyt Sep 21 '11 at 18:38
  • After trying tones of ways, this is the only solution that works for me in JBoss with redirect instead of forward – Raymond Chen Jan 24 '21 at 00:33
1

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.

Anthony O.
  • 22,041
  • 18
  • 107
  • 163
  • 1
    So you're now forwarding instead of redirecting. For that you don't need the `Flash#setKeepMessages()` anymore at all. Or were you actually not using it at all? – BalusC Sep 28 '12 at 11:06
  • You're right, I stopped using it :) Actually, I found that when I encountered the bug [WELD-1218](https://issues.jboss.org/browse/WELD-1218). – Anthony O. Sep 28 '12 at 13:02
0

JSFMessages are kept only for the processing of the actual request. A second request is made when using redirect, so the JSFMessages will be lost. The EL-Flash is a way to work around this. This example should work: http://ocpsoft.com/java/persist-and-pass-facesmessages-over-page-redirects/

tasel
  • 629
  • 5
  • 15
  • 3
    Did you miss the 1st paragraph about `setKeepMessages(true)` and the line `getFlash().setKeepMessages(true);` the code snippet of the question? He is **already** using Flash, but it does not work properly in combination with `ExternalContext#redirect()`. – BalusC Sep 22 '11 at 13:41