当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

The JSF 2.0 exception handling mechanism

发表于: 2013-11-17   作者:sunjing   来源:转载   浏览次数:
摘要: 《JSF 2.0 Cookbook》 http://www.packtpub.com/article/jsf-20-features In this recipe we talk about the exception handling mechanism provided by JSF 2.0. You will see how to map exceptions to error

《JSF 2.0 Cookbook》 http://www.packtpub.com/article/jsf-20-features

In this recipe we talk about the exception handling mechanism provided by JSF 2.0. You will see how to map exceptions to error pages in the web.xml file, how to use a managed bean for extracting an exception from the request and build a String from the stack trace, and how to customize the exception handling with a user-defined exception handler.

How to do it...

We start our recipe with the simplest solution for handling exceptions. It consists in mapping exception to error pages in the web.xml descriptor . For start we add in web.xml an entry for to define a JSF page as an error page (in our example, we define an error page named error.xhtml, mapped to the java.lang.NumberFormatException exception):

...
<error-page>
<exception-type>java.lang.NumberFormatException</exception-type>
<location>/faces/error.xhtml</location>
</error-page>

...

Now, JSF keeps track of a set of values in the request that provide more details on the error page. Next you can see one of these values edited in error.xhtml:

...
User Error:
#{requestScope['javax.servlet.error.message']}
...

Now, we can test the previous example by throwing a java.lang.NumberFormatException from a bean getter method, as shown next (when the error is thrown, the error.xhtml error page is getting into action):

...
private String number = "345s";
...
public String getNumber() {
try {
Integer intnumber = Integer.valueOf(this.number);
return String.valueOf(intnumber);
} catch (NumberFormatException e) {
throw new java.lang.NumberFormatException(e.getMessage());
}
}

public void setNumber(String number) {
this.number = number;
}
...

Going further, we can write a managed bean for extracting the exception from the request and building a String from the stack trace. You can see the action that does this job for us next:

...
private String error = "";
...
public String getError() {
StringBuilder errorMessage = new StringBuilder();
FacesContext facesContext = FacesContext.getCurrentInstance();
Map<String, Object> map =
facesContext.getExternalContext().getRequestMap();
Throwable throwable = (Throwable)
map.get("javax.servlet.error.exception");

if (throwable != null) {
errorMessage.append(throwable.getMessage()).append("\n");
for (StackTraceElement element : throwable.getStackTrace())
{
errorMessage.append(element).append("\n");
}
}
this.error = errorMessage.toString();
return this.error;
}
...

To get the stack trace we use the following code in the error.xhtml page:

...
System Administrator Error: <br/>
<h:outputText value="#{bean.error}"/>
...

You can go even further and customize the exception handling. Any custom exception handler should be defined in faces-config.xml, as in the following example:

...
<factory>
<exception-handler-factory>
exception.handler.CustomExceptionHandler
</exception-handler-factory>
</factory>
...

In the custom exception handler you should override the handle method to describe the behavior of your application in the case of a particular exception or set of exceptions. The prototype of this method is:

public void handle() throws FacesException {
...//do your job here
super.handle();
}

How it works...

Basically, in all three cases described previously, the idea is the same. The exceptions are caught by the system and they are treated according to our desires. We can provide a simple error page, or we can get much deeper and exploit the exception's stack trace and create large logs with detailed information for users or for administrators.

 

javax.faces.webapp.FacesServlet

public void service(ServletRequest req, ServletResponse resp)
        throws IOException, ServletException {

        ...

        // Acquire the FacesContext instance for this request
        FacesContext context = facesContextFactory.getFacesContext
              (servletConfig.getServletContext(), request, response, lifecycle);

        ....

}

 

com.sun.faces.context.FacesContextFactoryImpl

public FacesContext getFacesContext(Object sc, Object request, Object response, Lifecycle lifecycle)
    throws FacesException {

       ....        
        FacesContext ctx = new FacesContextImpl(
                  externalContextFactory.getExternalContext(sc, request, response), lifecycle);

        ctx.setExceptionHandler(exceptionHandlerFactory.getExceptionHandler());

        return ctx;        
    }

 

com.sun.faces.lifecycle.Phase 

public void doPhase(FacesContext context,
                        Lifecycle lifecycle,
                        ListIterator<PhaseListener> listeners) {

    ....

    try {
            handleBeforePhase(context, listeners, event);
            if (!shouldSkip(context)) {
                execute(context);
            }
        } catch (Throwable e) {
            queueException(context, e);
        } finally {
            try {
                handleAfterPhase(context, listeners, event);
            } catch (Throwable e) {
                queueException(context, e);
            }
            ....

            context.getExceptionHandler().handle();
        }

}

 

JSF 2.0 default impl

com.sun.faces.context.ExceptionHandlerFactoryImpl

public ExceptionHandler getExceptionHandler() {
        FacesContext fc = FacesContext.getCurrentInstance();
        if (fc.getPartialViewContext().isAjaxRequest()) {
            return new AjaxExceptionHandlerImpl(new ExceptionHandlerImpl(Boolean.TRUE));
        }
        ApplicationAssociate associate = getAssociate(fc);
        return new ExceptionHandlerImpl(((associate != null) ? associate.isErrorPagePresent() : Boolean.TRUE));
    }

The JSF 2.0 exception handling mechanism

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
http://www.softwaretestinghelp.com/exception-handling-framework-selenium-tutorial-19/ In last
From : http://www.codeproject.com/Articles/731913/Exception-Handling-in-MVC Introduction In t
对于所有的开发人员来说,Exception Handling是我们每天都要面对的事情。对于基于Source Code的Exce
对于所有的开发人员来说,Exception Handling是我们每天都要面对的事情。对于基于Source Code的Exce
One of the problems with exception handling is knowing when and how to use it. In this articl
三、 TRY CATCH & Return 在上面一节中,我通过RAISERROR重写了创建User的Stored procedure,实际上
三、 TRY CATCH & Return 在上面一节中,我通过RAISERROR重写了创建User的Stored procedure,实际上
原文: 黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block
在任何Application的开发中,对不可预知的异常进行troubleshooting时,异常处理显得尤为重要。对于
在任何Application的开发中,对不可预知的异常进行troubleshooting时,异常处理显得尤为重要。对于一
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号