0

重複の可能性:
preRender からのナビゲーション後に顔のメッセージを保持できない

ユーザー アカウントの検証が完了したら、リダイレクトを実行したいと考えています。また、FacesMessage を使用して、ユーザー アカウントの検証に何が起こったかを報告したいと考えています。リダイレクトは正常に機能していますが、リダイレクト全体で FacesMessage が維持されていません。

これが私のコードです。

validateAccount.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>

MessageHandler.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 を使用して、リダイレクト時にメッセージを保持しています。この問題を解決できるように、これを変更できますか?

4

0 に答える 0