javahttp-redirectjsf-2messageflash-scope

Send flash message to redirected page


I have one page where I add new item when I save data I go to other page where I want to see message that "Data was saved successful". I read that message belong to request scope that's why I am using the flash scope.

context.addMessage("calendarGameForm:growl", new FacesMessage("Data was saved successful");
context.getExternalContext().getFlash().setKeepMessages(true);
return outcome;

This is invoked by a save button on the first page addSeason.xhtml.

<p:commandButton id="save" action="#{controller.add}"
                 value="#{msg.save}" ajax="true"
                 type="submit" update="@form"/>

The navigation rule is definied as follows.

<navigation-rule>
    <from-view-id>/competitions/addSeason.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>success</from-outcome>
        <to-view-id>/competitions/calendarGame.xhtml</to-view-id>
        <redirect/>
    </navigation-case>
    <navigation-case>
        <from-outcome>fail</from-outcome>
        <to-view-id>/competitions/calendarGame.xhtml</to-view-id>
        <redirect/>
    </navigation-case>
</navigation-rule>

But in the target page calendarGame.xhtml I don't get my message.

<p:growl id="growl" globalOnly="true" sticky="true"/>

I am also getting this warning in the server log.

WARNING [javax.enterprise.resource.webcontainer.jsf.flash] (ajp--127.0.0.1-8009-1) JSF1095: The response was already committed by the time we tried to set the outgoing cookie for the flash. Any values stored to the flash will not be available on the next request.

How I can solve my problem?


Solution

  • This problem is two-fold.

    As to the first problem, I gather that you're adding the faces message in an action method and not during prerender view, right? You've there a return outcome, so it'd make sense that it's inside a real action method. In that case, the JSF1095 message is caused by an Ajax-encoding related bug in Mojarra and fixed in 2.1.11. Mojarra have had a lot of Flash scope related problems of which the last one is fixed in 2.1.14. I recommend to update Mojarra to at least that version. It's currently already at 2.1.17.

    As to the second problem, a <p:growl globalOnly="true"> shows only messages with a null client ID. Fix your addMessage() call to pass null instead.

    context.addMessage(null, message);
    

    If you were actually adding the faces message during prerender event before a navigation, then the problem would have a different cause and would require a different solution. See among others this answer: Mojarra 2.1.14 flash scope messages and redirect to different path.