jsfjsf-2primefacesflash-scope

JSF Flash Scope remembers too many messages


I have a simple flow in my appliaction - if you fill out and press save on one form (if everything goes well) you are redirected to a second view with a list. Now I wanted to add a message saying "You successfuly added an object" but since I'm using a redirect from what I remember I need to use the Flash scope. And so I did. The problem is that during the first "save" it correctly shows only 1 message but when I navigate back to the form and hit "save" it will show me the current message and the old one! It's even stranger that when (for the 3rd time) I go back to the form and hit "save" I again get only 1 message (and so on 1-2-1-2-1-2 etc...). Am I doing something wrong or is it a bug in jsf? I mean my I'm calling the same method and get different results...

I'm using primefaces and the newest mojarr:

jsf-api-2.1.1-b04
jsf-impl-2.1.1-b04
primefaces-2.2.1

Here's the code (most relevant parts at least):

SaveForm.xhtml:

    <div id="content-box" class="content-box">
        <p:panel id="content-panel" header="Dane raportu"
            styleClass="content-panel">
            <div class="content-box">
                <h:form prependId="false">
                    <h:panelGrid id="grid" columns="2" styleClass="content-panel">

                        <!-- some inputs and labels -->

                        <p:commandButton value="#{msg['thesis.save.button']}"
                            action="#{thesisBean.saveThesis}" />    
                    </h:panelGrid>
                </h:form>
            </div>
        </p:panel>
    </div>

saveThesis method:

public String saveThesis() {
            //this creates a Hibernate entity and saves it to the DB
    thesisService.addThesis(createThesisEntity());

    FacesContext context = FacesContext.getCurrentInstance();
    context.getExternalContext().getFlash().setKeepMessages(true);
    ResourceBundle bundle = context.getApplication().getResourceBundle(
            context, "msg");

    context.addMessage(null,
            new FacesMessage(FacesMessage.SEVERITY_INFO, "key1",
                    "key2"));


    return "list-theses.xhtml?faces-redirect=true";
}

list-theses.xhtml:

<ui:composition template="/basicTemplate.xhtml">

<ui:define name="content">

    <p:growl id="growl" showDetail="true" sticky="false" life="5000" />

    <div id="content-box" class="content-box">
        <h:form prependId="false" id="table-form">
            <p:dataTable var="thesis" value="#{thesisTableBean.theses}"
                paginator="true" rows="20">

                <p:column styleClass="table-name-column">
                    <f:facet name="header">
                        <h:outputText value="#{msg['thesis.table.name.header']}" />
                    </f:facet>
                    <h:outputText value="#{thesis.firstName} #{thesis.lastName}" />
                </p:column>

                <p:column>
                    <f:facet name="header">
                        <h:outputText value="#{msg['thesis.table.title.header']}" />
                    </f:facet>
                    <h:outputText value="${thesis.title}" />
                </p:column>

                <p:column styleClass="table-number-column">
                    <f:facet name="header">
                        <h:outputText value="#{msg['thesis.table.number.header']}" />
                    </f:facet>
                    <h:outputText value="${thesis.number}" />
                </p:column>

            </p:dataTable>

        </h:form>

    </div>

</ui:define>


Solution

  • Well I found a "solution" here: http://ocpsoft.com/java/persist-and-pass-facesmessages-over-page-redirects/ Seems to work pretty well. Still I have no idea why my code isn't working. I mean it's the same method every time but the result differs...