我在应用程序中有一个简单的流程 - 如果您填写并按下保存在一个表单上(如果一切顺利),您将被重定向到带有列表的第二个视图。现在我想添加一条消息,说“你成功添加了一个对象”,但是因为我使用了我记得需要使用Flash范围的重定向。所以我做到了。问题是,在第一次“保存”时,它正确显示只有1条消息,但是当我回到表单并点击“保存”时,它会显示当前消息和旧消息!更奇怪的是,当(第三次)我回到表格并点击“保存”时,我只能得到1条消息(等1-2-1-2-1-2等)。我做错了什么或者它是jsf中的错误?我的意思是我的。我打电话同样的方法,并得到不同的结果...JSF Flash Scope记得太多的消息
我使用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";
}
列表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>
这是在这里回答:http://stackoverflow.com/questions/21168059/jsf-messages-persistance/21230965?noredirect=1#21230965 – 2014-01-21 09:42:07