You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by Apache Wiki <wi...@apache.org> on 2009/12/25 02:05:17 UTC

[Myfaces Wiki] Update of "Handling_Server_Errors" by ValBlant

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Myfaces Wiki" for change notification.

The "Handling_Server_Errors" page has been changed by ValBlant.
http://wiki.apache.org/myfaces/Handling_Server_Errors?action=diff&rev1=25&rev2=26

--------------------------------------------------

  
  In the backing bean we construct a message which informs the user that something we didnt't plan for has happened with directions on who to call, what to do etc. We don't really care if they actually do cut-and-paste the error and email it to us as it is also in Tomcat's logs but giving the user something to do and become part of resolving the problem is always a good idea :)
  
+ ==== ViewExpiredException: No saved view state could be found for the view identifier ====
+ 
+ In some configurations, you might run into this problem exception with the error handling method shown above. This will happen if an error results in a forward, rather than redirect. The ''ViewHandler'' will call ''response.sendError()'' in case of an error, which will lookup your ''<error-page>'' declarations in ''web.xml'' and forward to the error url. If the error url is picked up by the ''FacesServlet'' (i.e. it's a JSF url), a new JSF lifecycle will be started. However, since this is a forward, the request object will still contain all of the request parameters, including ''"javax.faces.ViewState"'', which makes the request look like a postback to the ''ViewHandler''. Since it's a postback, the ''ViewHandler'' will expect a saved view, which is clearly not going to be there, since our ''viewId'' is now referencing the error page.
+ 
+ This problem can be solved by customizing the ''ViewHandler'' to use ''response.sendRedirect()'' instead of ''response.sendError()'', but that will mean that we can no longer use ''web.xml'' for specifying the error page mappings.
+ 
+ Another way to handle this would be to use an intermediate step by specifying a non-JSF URL as the error page and then somehow redirecting to the JSF error page. For example, for the 404 error code you could specify ''/error/404_redirect.html'':
+ {{{
+ <html><head><meta http-equiv="Refresh" content="0; URL=/DPSV4/error/404.jsf"></head></html>
+ }}} 
+ 
+ This works, but requires you to hard code the context path.
+ 
+ The solution I ended up with involves a ''RedirectServlet'':
+ {{{
+ public class RedirectServlet extends HttpServlet {
+ 	private static final String URL_PREFIX = "url=";
+ 
+ 	@Override
+ 	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ 		String query = req.getQueryString();
+ 		if ( query.contains(URL_PREFIX) ) {
+ 			String url = query.replace(URL_PREFIX, "");
+ 			if ( !url.startsWith(req.getContextPath()) ) {
+ 				url = req.getContextPath() + url;
+ 			}
+ 			
+        		resp.sendRedirect(url);
+ 		}
+ 	}
+ 
+ 	@Override
+ 	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ 		doGet(req, resp);
+ 	}
+ 
+ }
+ 
+ }}}
+ 
+ used like this in ''web.xml'':
+ {{{
+ 	<servlet>
+ 	    <servlet-name>Redirect Servlet</servlet-name>
+ 	    <servlet-class>ca.gc.agr.ops.web.jsf.redirect.RedirectServlet</servlet-class>
+ 	</servlet>    
+ 	<servlet-mapping>
+ 	    <servlet-name>Redirect Servlet</servlet-name>
+ 	    <url-pattern>/redirect</url-pattern>
+ 	</servlet-mapping>
+ 
+ 	<error-page>
+ 		<error-code>403</error-code>
+ 		<location>/redirect?url=/error/403.jsf</location>
+ 	</error-page>
+ 	<error-page>
+ 		<error-code>404</error-code>
+ 		<location>/redirect?url=/error/404.jsf</location>
+ 	</error-page>
+ 	<error-page>
+ 		<error-code>500</error-code>
+ 		<location>/redirect?url=/error/500.jsf</location>
+ 	</error-page>
+ 
+ }}}
  
  == With plain JSP ==