JSF Flash Scope 记住太多消息

发布于 2024-11-04 21:56:06 字数 3371 浏览 3 评论 0原文

我的应用程序有一个简单的流程 - 如果您在一个表单上填写并按“保存”(如果一切顺利),您将被重定向到带有列表的第二个视图。现在我想添加一条消息“您成功添加了一个对象”,但由于我使用的是我记得的重定向,因此我需要使用 Flash 范围。我也这么做了。问题是,在第一次“保存”期间,它仅正确显示 1 条消息,但当我导航回表单并点击“保存”时,它将显示当前消息和旧消息!更奇怪的是,当我(第三次)返回表单并点击“保存”时,我再次只收到 1 条消息(依此类推 1-2-1-2-1-2 等...)。是我做错了什么还是jsf的bug?我的意思是我调用相同的方法并得到不同的结果...

我正在使用 primefaces 和最新的 mojarr:

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

这是代码(至少是最相关的部分):

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方法:

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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风无影 2024-11-11 21:56:07

好吧,我在这里找到了一个“解决方案”: http://ocpsoft .com/java/persist-and-pass-facesmessages-over-page-redirects/ 似乎工作得很好。我仍然不知道为什么我的代码不起作用。我的意思是每次都是相同的方法,但结果不同......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文