2

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 :) )

luke_leo
  • 55
  • 3
  • 8
  • 2
    What happens if you set the flash message in an action method and return a page with "faces-redirect=true" from that? – Mike Braun Oct 04 '12 at 08:35
  • @MikeBraun this is what i was trying right now..Strange things happen :).. i put 2 actions in 2 commandbuttons each one setting a message and one redirecting to index.xhtml and the other to test.xhtml using the above "configuration". And it works the message appears on index.html as it should.. but why the same 2 lines of code don't work if they are in the constructor?? – luke_leo Oct 04 '12 at 08:55
  • I don't know really, uhm, what about a PostConstruct annotated method? Does that work? – Mike Braun Oct 04 '12 at 09:03
  • 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.. – luke_leo Oct 04 '12 at 09:13
  • If `preRenderView` doesn't work then `@PostConstruct` will very likely not work as well. – dexter meyers Oct 04 '12 at 09:29

1 Answers1

3

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    great answer!! and indeed it works!! Omnifaces to the rescue! on the side note is there any way to bypass before invoking postInvokeAction? does it have to do with omnifaces code or is it a JSF-restriction? thanks again!! – luke_leo Oct 05 '12 at 06:51
  • You're welcome. It's a JSF restriction. Without it, the apply request values until with invoke action phases won't be entered. – BalusC Oct 05 '12 at 10:30