ke3pup
ke3pup

Reputation: 1895

Handling viewExpiredexception by way of redirection

I've been trying to cache and handle viewExpiredexception where if a `viewExpiredexceptionviewExpiredexception is throw, i have written a custom viewExpiredexception Handler which is suppose to redirect back to the page where the Exception is thrown, i also insert a boolean into session which is used in the redicted page to show "page was refreshed" message.

Below is the relevant part of my ViewExpiredException Handler:

     public void handle() throws FacesException {
            for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
                ExceptionQueuedEvent event = i.next();
                ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
                Throwable t = context.getException();
                if (t instanceof ViewExpiredException) {
                    ViewExpiredException vee = (ViewExpiredException) t;
                    FacesContext fc = FacesContext.getCurrentInstance();
                    Map<String, Object> requestMap = fc.getExternalContext().getRequestMap();
                    NavigationHandler nav =  fc.getApplication().getNavigationHandler();
                    try {
                        requestMap.put("currentViewId", vee.getViewId());
                        HttpServletRequest orgRequest = (HttpServletRequest) fc.getExternalContext().getRequest();
                        fc.getExternalContext().redirect(orgRequest.getRequestURI());
                        Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
                        sessionMap.put("refreshedMaxSesion",true);
                        fc.renderResponse();
                    }
                    catch(IOException e){   }
                    finally {   i.remove(); }
                }
            }
            // here queue will not contain any ViewExpiredEvents, let the parent handle them.
            getWrapped().handle();
      }

and the Navigation case

           <navigation-rule>
              <navigation-case>
               <from-outcome>/app/ord1</from-outcome>
               <to-view-id>/jsp/orderHist.jsp</to-view-id>
               <redirect />
              </navigation-case>
            </navigation-rule>

I had limited success with the above, it would work in some cases but in other cases the page wouldn't redirect at all. It works mostly in chrome but in IE it didn't work at all.

I tried making few changes such as using

 HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
       response.sendRedirect(vee.getViewId());

but i was getting the 500 error pages with exception saying View must Exists... so i stopped experimenting to find out what i am doing wrong first. How can this goal of cahcing a ViewExpiredException, and Redirectign back to the page where the error was thrown be archived?

I'm on myFaces (2.1.7) and richFaces (3.3.3). Thanks

Upvotes: 1

Views: 667

Answers (1)

lu4242
lu4242

Reputation: 2318

There is some work already done inside MyFaces Community to deal with ViewExpiredException in a graceful way. Some issues has been solved in MyFaces Core (see MYFACES-3530 and MYFACES-3531) Maybe you should try the latest snapshot HERE.

Upvotes: 1

Related Questions