Mojarra 2.1.14 flash scope messages and redirect to different path
Asked Answered
V

1

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

Verge answered 4/10, 2012 at 8:19 Comment(5)
What happens if you set the flash message in an action method and return a page with "faces-redirect=true" from that?Fleurdelis
@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??Verge
I don't know really, uhm, what about a PostConstruct annotated method? Does that work?Fleurdelis
i just used <f:event type="preRenderView" > 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..Verge
If preRenderView doesn't work then @PostConstruct will very likely not work as well.Kkt
F
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:

Femi answered 4/10, 2012 at 15:2 Comment(2)
great answer!! and indeed it works!! Omnifaces to the rescue! on the side note is there any way to bypass <f:viewParam name="dummy" /> before invoking postInvokeAction? does it have to do with omnifaces code or is it a JSF-restriction? thanks again!!Verge
You're welcome. It's a JSF restriction. Without it, the apply request values until with invoke action phases won't be entered.Femi

© 2022 - 2024 — McMap. All rights reserved.