exception management...
agallo73 Mar 8, 2010 8:50 AMHello, I'd like to use a proper approach for Exception handling.
I'd like to catch all the possible exceptions in a centralized place and then redirect the normal flow to a facelet if some error occurrs.
I saw many articles using filters, phase listeners and redirecting to a jsp page but I did not find anything as far as facelets are concerned.
The web flow of my application happens within a wizard and I'd like to keep the flow within the wizard also when an exception occurs.
May you provide some code or technical article to point to?
When Ajax rendering is concerned exception management is more complicated and I think that there should be a good strategy to follow.
May some one provide some hint please?
This is the code in an article I found...but it works for jsp. How mcan I change it to work with facelets?
public class FacesServletWrapper extends HttpServlet
{
private FacesServlet facesServletDelegate;
private static final String ERROR_PAGE = "serverErrorPage";
private String serverErrorPage;
public void init(ServletConfig servletConfig) throws ServletException
{
facesServletDelegate= new FacesServlet();
facesServletDelegate.init(servletConfig);
serverErrorPage = servletConfig.getInitParameter(ERROR_PAGE);
}
public void destroy()
{
facesServletDelegate.destroy();
}
public ServletConfig getServletConfig()
{
return facesServletDelegate.getServletConfig();
}
public String getServletInfo()
{
return facesServletDelegate.getServletInfo();
}
private FacesContext getFacesContext(final ServletRequest request, final ServletResponse response) {
FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext != null) {
return facesContext;
}
FacesContextFactory contextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder. FACES_CONTEXT_FACTORY);
LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder. LIFECYCLE_FACTORY);
Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory. DEFAULT_LIFECYCLE);
facesContext = contextFactory.getFacesContext(this.getServletContext(), request, response, lifecycle);
UIViewRoot view = facesContext.getApplication().getViewHandler().createView(facesContext, "yourOwnID");
facesContext.setViewRoot(view);
return facesContext;
}
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException
{
try
{
facesServletDelegate.service(request,response);
}
catch(Throwable e)
{
System.out.println("eccezione intercettata");
redirectToServerErrorPage((HttpServletRequest) request,
(HttpServletResponse) response);
/*
FacesContext facesCtx = getFacesContext(request,response);
Application app = facesCtx.getApplication();
ELContext elContext = FacesContext.getCurrentInstance().getELContext();
TrasformBean trasformBean = (TrasformBean) app.getELResolver()
.getValue(elContext, null, "trasformBean");
NavigationHandler navigationHandler = app.getNavigationHandler();
trasformBean.setViewId("/main.xhtml");
navigationHandler.handleNavigation(facesCtx, null,
"exceptionCaught");
facesCtx.renderResponse();
*/
}
}
private void redirectToServerErrorPage(HttpServletRequest request,
HttpServletResponse response) throws IOException
{
if (!serverErrorPage.equals(""))
{
response.sendRedirect(request.getContextPath() + serverErrorPage);
}
}
}