Skip to main content

Redirect from preRenderView doesn't maintain FacesMessages

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
2 replies [Last post]
slominskir
Offline
Joined: 2010-11-15

I want to do a redirect in the case a validation failure occurs in one of my view params. I also would like to use a FacesMessage to report what happend to the user. I can get the redirect to work, but the FacesMessage is not maintained across the redirect. I'm using the code below:

    public void preRenderView(ComponentSystemEvent event) {
        FacesContext fc = FacesContext.getCurrentInstance();

        boolean getMethod = ((HttpServletRequest) fc.getExternalContext().getRequest()).getMethod().equals("GET") ? true : false;
        boolean ajaxRequest = fc.getPartialViewContext().isAjaxRequest();
        boolean validationFailed = fc.isValidationFailed();

        if (getMethod && !ajaxRequest && validationFailed) {
            NavigationHandler nav = fc.getApplication().getNavigationHandler();
            Flash flash = fc.getExternalContext().getFlash();
            flash.setKeepMessages(true);
            fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "My error message goes here", ""));
            nav.handleNavigation(fc, null, "/index?faces-redirect=true");
            fc.renderResponse();
        } else {
            loadEntity();
        }
    }

I am using extremely similar code in my custom exception handler and it works as expected (the FacesMessage is maintained across the redirect). Is this a known issue with the preRenderView system event? The custom exception handler code looks like:

    @Override
    public void handle() throws FacesException {
        for(Iterator<exceptionqueuedevent></exceptionqueuedevent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
            ExceptionQueuedEvent event = i.next();
            ExceptionQueuedEventContext context = (ExceptionQueuedEventContext)event.getSource();
            Throwable t = context.getException();

            if(t instanceof ViewExpiredException) {
                FacesContext fc = FacesContext.getCurrentInstance();
                NavigationHandler nav = fc.getApplication().getNavigationHandler();
                Flash flash = fc.getExternalContext().getFlash();
                try {
                    flash.setKeepMessages(true);
                    fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, &quot;Your session has expired&quot;, &quot;&quot;));
                    nav.handleNavigation(fc, null, &quot;/index?faces-redirect=true&quot;);
                    fc.renderResponse();
                }
                finally {
                    i.remove();
                }
            }
        }

        getWrapped().handle();
    }

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
73 Guest
Offline
Joined: 2010-11-15

It looks like Flash scope is not production quality.  I've run into this
error now: 

>WARNING: JSF1095: The response was already committed by the time we tried to
>set the outgoing cookie for the flash.  Any values stored to the flash will
>not be available on the next request.
>
I guess it has something to do with mixing AJAX and Flash scope?  This
error is discussed here:

http://192.9.162.102/thread.jspa?threadID=5445714 [1]

It looks like some people are rolling their own solution for maintaining
FacesMessages across requests:

http://ocpsoft.com/java/persist-and-pass-facesmessages-over-page-redirects/
[2]

[1] http://192.9.162.102/thread.jspa?threadID=5445714
[2]
http://ocpsoft.com/java/persist-and-pass-facesmessages-over-page-redirects/

slominskir
Offline
Joined: 2010-11-15

It looks like Flash scope is not production quality. I've run into this error now:

WARNING: JSF1095: The response was already committed by the time we tried to set the outgoing cookie for the flash. Any values stored to the flash will not be available on the next request.

I guess it has something to do with mixing AJAX and Flash scope? This error is discussed here:
http://192.9.162.102/thread.jspa?threadID=5445714
It looks like some people are rolling their own solution for maintaining FacesMessages across requests:
http://ocpsoft.com/java/persist-and-pass-facesmessages-over-page-redirects/