I have a problem with JSF's FlashScope, note that I'm aware that the redirect should point to the same base path as the calling page.
My case, the action will be initialized when a client click a link from his/her email, then it will load a .xhtml (has a preRenderView event) page with a backing bean to check the parameters passed. When the parameters are wrong it must redirect to a new page with message.
The View:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<f:metadata>
<f:viewParam name="dummy" />
<f:event listener="#{signupMaterialBean.initFromContext()}"
type="preRenderView"></f:event>
</f:metadata>
<h:outputText value="#{signupMaterialBean.html}" escape="false"></h:outputText>
</ui:composition>
The Backing Bean:
public void initFromContext() {
try {
Map<String, String> params = facesContext.getExternalContext()
.getRequestParameterMap();
...
} catch (NullPointerException e) {
try {
Message message = new Message();
String msg = "Invalid parameters";
message.setMessage(msg);
JSFUtils.flashScope().put("message", message);
facesContext.getExternalContext().redirect("message.xhtml");
facesContext.responseComplete();
} catch (IOException ioe) {
}
} catch (IOException e) {
}
}
The redirect works, but the message save in FlashScope is gone. Any idea why the flash scope is being deleted or another way to do it?
message.xhtml
<ui:define name="head"></ui:define>
<ui:define name="content">
<div class="box-middle-content faded">
<div class="message">
<h:outputText escape="false" value="#{flash.message.message}"></h:outputText>
<br />
<p>
<h:outputText value="#{msgs['message.footer']}" escape="false" />
</p>
</div>
</div>
</ui:define>
</ui:composition>
JSFUtils
public class JSFUtils {
public static Flash flashScope() {
return (FacesContext.getCurrentInstance().getExternalContext()
.getFlash());
}
}
Updated view:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:of="http://omnifaces.org/functions">
<f:metadata>
<f:viewParam name="dummy"></f:viewParam>
<f:event type="postInvokeAction"
listener="#{signupMaterialBean.initFromContext}" />
</f:metadata>
<h:outputText value="#{signupMaterialBean.html}" escape="false"></h:outputText>
</ui:composition>