jsf-2primefacesmojarraomnifacesflash-scope

Mojarra 2.1.14 flash scope messages and redirect to different path


According to this: http://java.net/jira/browse/JAVASERVERFACES-2136 flash-"scoped" messages should survive a redirect to a page on a different path.. I wanted to use something like this in my application so i downloaded javax.faces-2.1.14-20121003.074348-10 snapshot from here https://maven.java.net/content/repositories/snapshots/org/glassfish/javax.faces/2.1.14-SNAPSHOT/ to test.

My situation is this: I have a page (call it test.xhtml) in the root directory that in the view-scoped backing bean during the call of the constructor does a check and conditionally sets a message using Omnifaces Message.addFlashGlobalInfo and redirects to index.xthml also in the root directory using Omnifaces Faces.Redirect() (thanks BalusC!). In index.xhtml i have a Primefaces

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

I use the same "configuration" described above in other pages as well and it works fine when the redirect is done to the same page called the bean method.

So shouldn't the message survive the different path redirect or did i misunderstood something about this issue?? maybe there is something else wrong here??

Thanks in advance! (i'm looking forward hearing BalusC opinion on this btw :) )


Solution

  • i just used to call an init method that does sets message and redirects but again no message appears!! so i don't think PostConstruct will work either..

    Indeed, the <f:event type="preRenderView"> is too late to set a flash message. The flash scope can't be created when JSF is currently sitting in render response phase. You basically need to set the flash message before render response phase. In spite of the name preRenderView, this event is actually fired during (the very beginning of) the render response phase.

    The @PostConstruct may be on time, provided that it's not been called during render response. This however won't work very well together with <f:viewParam>.

    To fix this, as you're using OmniFaces already, just use <f:event type="postInvokeAction">.

    <f:metadata>
        <f:viewParam name="some" value="#{bean.some}" />
        <f:event type="postInvokeAction" listener="#{bean.init}" />
    </f:metadata>
    

    See also: