JSF Flash Scope记住太多消息 [英] JSF Flash Scope remembers too many messages
问题描述
我的工作流程很简单-如果填写并在一个表单上按Save(如果一切顺利),您将被重定向到带有列表的第二个视图.现在,我想添加一条消息,说您成功添加了一个对象",但是由于我使用的是我记得的重定向,因此需要使用Flash范围.所以我做到了.问题在于,在第一个保存"期间,它仅正确显示一条消息,但是当我导航回表单并单击保存"时,它将向我显示当前消息和旧消息!甚至更奇怪的是,当我(第3次)返回表格并单击保存"时,我再次仅收到1条消息(以此类推,例如1-2-1-2-1-2等).我是在做错什么还是jsf中的错误?我的意思是我正在调用相同的方法并获得不同的结果...
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...
我正在使用素数和最新的mojarr:
I'm using primefaces and the newest mojarr:
jsf-api-2.1.1-b04
jsf-impl-2.1.1-b04
primefaces-2.2.1
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:
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方法:
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:
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>
推荐答案
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...
这篇关于JSF Flash Scope记住太多消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!