ViewExpiredException
custom exception handler (available in JSF 2.0) can handle this case, but I had a need for another type of "Session" object to be monitored to determine if I should redirect based on its status. The other object was stored in the HttpSession
object as an attribute so I decided to handle it with a Filter
(@WebFilter
).The first thing is to determine if the request is a
partial/ajax
request. If it is a normal post, we can handle it with a HttpResponse.sendRedirect(String location)
mechanism. If it is AJAX, we need to handle it in a completely different manner.
1 2 3 4 5 6 7 8 | private boolean isAJAXRequest(HttpServletRequest request) { boolean check = false ; String facesRequest = request.getHeader( "Faces-Request" ); if (facesRequest != null && facesRequest.equals( "partial/ajax" )) { check = true ; } return check; } |
Note: This is being intercepted in a Filter so I don't have access to the FacesContext. Here is a partial code snippet of how to send the redirect. You would need to set the variable
TARGET
to go to the desired location.
1 2 3 4 5 6 7 8 9 10 11 12 | String redirectURL = response.encodeRedirectURL(request.getContextPath() + TARGET); if (isAJAXRequest(request)) { StringBuilder sb = new StringBuilder(); sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><partial-response><redirect url=\"" ).append(redirectURL).append( "\"></redirect></partial-response>" ); response.setHeader( "Cache-Control" , "no-cache" ); response.setCharacterEncoding( "UTF-8" ); response.setContentType( "text/xml" ); PrintWriter pw = response.getWriter(); pw.println(sb.toString()); pw.flush(); } |