I want to use PRG pattern introduced in JSF 2.0
But the drawback of using it is the massages added during executing the action is lost in the result output.
To resolve this issue I use
FacesContext.getCurrentInstance().getExternalContext().getFlash()
.setKeepMessages(true);
Meaning that new JSF 2.0 capability Flash will keep this messages for the result view which will redirected after my POST request will be handled.
The messages really appears on the result page but then when I go to any other view and return back to this one I still see that massage again one additional time.
Is it the JSF bug?
How can I make it to be shown just once after the handling the action?
My code:
package test;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
@ManagedBean
@RequestScoped
public class TestBean {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String doSomething() {
FacesContext.getCurrentInstance().addMessage(
"",
new FacesMessage(FacesMessage.SEVERITY_INFO, "Just changed!!!",
""));
FacesContext.getCurrentInstance().getExternalContext().getFlash()
.setKeepMessages(true);
return "test.xhtml?faces-redirect=true";
}
}
The test page :
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/WEB-INF/templates/base.xhtml">
<ui:define name="head">
<title>Title</title>
</ui:define>
<ui:define name="content">
<h:messages showDetail="true" />
<h1>Test</h1>
<h:outputLabel value="Test Value" for="textField"/>
<h:inputText id="textField" value="#{testBean.text}" />
<h:commandButton action="#{testBean.doSomething}" value="Do The Action" />
</ui:define>
</ui:composition>
Some another page. (Really no matter what is it).
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/WEB-INF/templates/base.xhtml">
<ui:define name="head">
<title>Another one</title>
</ui:define>
<ui:define name="content">
<h1>Just another page</h1>
<h:messages showDetail="true" />
Hello there.
<h:outputLink value="test.xhtml">Back</h:outputLink>
</ui:define>
</ui:composition>
Thanks in advance.