可能重复:
从 preRender 导航后无法保留面部消息
完成用户帐户验证后,我想进行重定向。我还想使用 FacesMessage 报告用户帐户验证发生的情况。重定向工作正常,但 FacesMessage 不会在整个重定向中维护。
这是我的代码。
验证帐户.xhtml
<!DOCTYPE html>
<html lang="en" 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">
<h:head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</h:head>
<h:body>
<h:form acceptcharset="UTF-8" name="validateAccountActionForm" id="validateAccountActionForm" prependId="false">
<f:metadata>
<f:event type="preRenderView" listener="#{validateAccountAction.validateUserAccount}"></f:event>
</f:metadata>
</h:form>
</h:body>
</html>
ValidateAccountAction.java
@ManagedBean(name="validateAccountAction")
@RequestScoped
public class ValidateAccountAction {
public void validateUserAccount(){
------------;
------------;
performNavigation("accountValidationMessage.faces");
}
public void performNavigation(String pageName){
FacesContext ctx = FacesContext.getCurrentInstance();
Flash flash = ctx.getExternalContext().getFlash();
flash.setKeepMessages(true);
addMessage(null,"Congratulations! Your Account Successfully Validated.");
ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler)ctx.getApplication().getNavigationHandler();
nav.performNavigation(pageName+"?faces-redirect=true");
}
public void addMessage(String clientId, String infoMessage){
// if clientId is null then the message is applied for whole form.
FacesContext facesContext = FacesContext.getCurrentInstance();
Severity severityError = FacesMessage.SEVERITY_INFO;
FacesMessage facesMessage = new FacesMessage(infoMessage);
facesMessage.setSeverity(severityError);
facesContext.addMessage(clientId, facesMessage);
}
}
accountValidationMessage.xhtml
<!DOCTYPE html>
<html lang="en" 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">
<h:head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</h:head>
<h:body>
<h:form acceptcharset="UTF-8" name="validateSuccessForm" id="validateSuccessForm" prependId="false">
<h:messages style="padding-left: 10px;" infoClass="infoMsgs" errorClass="errMsgs" layout="table"/>
</h:form>
</h:body>
</html>
消息处理程序.java
public class MessageHandler implements PhaseListener {
private static final long serialVersionUID = 1250469273857785274L;
private static final String sessionToken = "MULTI_PAGE_MESSAGES_SUPPORT";
public PhaseId getPhaseId() {
return PhaseId.ANY_PHASE;
}
/*
* Check to see if we are "naturally" in the RENDER_RESPONSE phase. If we
* have arrived here and the response is already complete, then the page is
* not going to show up: don't display messages yet.
*/
// TODO: Blog this (MultiPageMessagesSupport)
public void beforePhase(final PhaseEvent event) {
FacesContext facesContext = event.getFacesContext();
this.saveMessages(facesContext);
if (PhaseId.RENDER_RESPONSE.equals(event.getPhaseId())) {
if (!facesContext.getResponseComplete()) {
this.restoreMessages(facesContext);
}
}
}
/*
* Save messages into the session after every phase.
*/
public void afterPhase(final PhaseEvent event) {
if (!PhaseId.RENDER_RESPONSE.equals(event.getPhaseId())) {
FacesContext facesContext = event.getFacesContext();
this.saveMessages(facesContext);
}
}
@SuppressWarnings("unchecked")
private int saveMessages(final FacesContext facesContext) {
List<FacesMessage> messages = new ArrayList<FacesMessage>();
for (Iterator<FacesMessage> iter = facesContext.getMessages(null); iter.hasNext();) {
messages.add(iter.next());
iter.remove();
}
if (messages.size() == 0) {
return 0;
}
Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
List<FacesMessage> existingMessages = (List<FacesMessage>) sessionMap.get(sessionToken);
if (existingMessages != null) {
existingMessages.addAll(messages);
} else {
sessionMap.put(sessionToken, messages);
}
return messages.size();
}
@SuppressWarnings("unchecked")
private int restoreMessages(final FacesContext facesContext) {
Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
List<FacesMessage> messages = (List<FacesMessage>) sessionMap.remove(sessionToken);
if (messages == null) {
return 0;
}
int restoredCount = messages.size();
for (Object element : messages) {
facesContext.addMessage(null, (FacesMessage) element);
}
return restoredCount;
}
}
面孔-config.xml
<lifecycle>
<phase-listener>
com.test.web.ssc.component.MessageHandler
</phase-listener>
</lifecycle>
这里的问题是页面重定向到 accountValidationMessage.faces,但消息没有显示。
我也使用这个类 MessageHandler.java 来保留重定向消息。我可以对此进行一些更改,以便我可以解决这个问题吗?