简体   繁体   English

在JSF应用程序中处理异常

[英]Handling exceptions in a JSF application

This may be too ambiguous of a question, but I'm looking for some help or best practices on handling exceptions in a JavaServer Faces application. 这可能是一个模棱两可的问题,但是我正在寻找有关JavaServer Faces应用程序中异常处理的帮助或最佳实践。

Right now, whenever I catch an exception, I log it and then throw another exception that I created, say MyCustomException . 现在,每当我捕获到异常时,我都会记录该异常,然后抛出另一个创建的异常,即MyCustomException This custom exception takes the Throwable object that was caught and emails the stack trace to me so that I know an exception has occurred. 此自定义异常将捕获到的Throwable对象发送给我,并将堆栈跟踪信息通过电子邮件发送给我,以便我知道已发生异常。 For now, this seems to work for me as far as being notified when an exception occurs. 就目前而言,就异常发生时得到通知而言,这似乎对我有用。

However, the trouble that I'm having is how to present the user with a pretty error page that lets them know that something went wrong and that I have been notified of the error. 但是,我遇到的麻烦是如何向用户显示一个漂亮的错误页面,该页面使他们知道出现了问题并且已将错误通知我。 Doing some searching, I've come across several articles that show how to redirect a user to another page using something similar to the following: 在进行搜索时,我遇到了几篇文章,这些文章展示了如何使用类似于以下内容的方法将用户重定向到另一个页面:

FacesContext context = FacesContext.getCurrentInstance();
ExternalContext extContext = context.getExternalContext();

String url = extContext.encodeActionURL(extContext.getRequestContextPath() +
    "/messages.faces");

extContext.redirect(url);

But this seems to be quite a bit to basically copy and paste for each caught exception, which doesn't exactly follow the DRY principle. 但是,对于每个捕获的异常,这似乎需要大量复制和粘贴,这并不完全遵循DRY原理。 In the end, I think what I would like to do is have an email sent to me whenever an exception occurs and then be able to send a message to a generic error page that then displays the message to the user. 最后,我想我想做的就是每当发生异常时都发送一封电子邮件给我,然后能够将消息发送到通用错误页面,然后将错误消息显示给用户。

Any ideas or best practices that I should follow? 我应该遵循的任何想法或最佳做法? Thank you in advance for any advice you can give! 预先感谢您提供的任何建议! I'm stumped. 我很沮丧

Note: If it helps, I'm using JSF 2.0 and Facelets. 注意:如果有帮助,我正在使用JSF 2.0和Facelets。

This urls could be useful for you: 该网址可能对您有用:

http://weblogs.java.net/blog/edburns/archive/2009/09/03/dealing-gracefully-viewexpiredexception-jsf2 http://weblogs.java.net/blog/edburns/archive/2009/09/03/dealing-gracefully-viewexpiredexception-jsf2

http://andyschwartz.wordpress.com/2009/07/31/whats-new-in-jsf-2/ http://andyschwartz.wordpress.com/2009/07/31/whats-new-in-jsf-2/

http://download.oracle.com/docs/cd/E17802_01/j2ee/javaee/javaserverfaces/2.0/docs/api/javax/faces/context/ExceptionHandler.html http://download.oracle.com/docs/cd/E17802_01/j2ee/javaee/javaserverfaces/2.0/docs/api/javax/faces/context/ExceptionHandler.html

Basically you must use new feature in JSF2.0, ExceptionHandler. 基本上,您必须在JSF2.0中使用ExceptionHandler的新功能。 This is a central point for handling unexpected exception. 这是处理意外异常的中心点。 You could indicate which page use for each exception type. 您可以指示每种异常类型使用哪个页面。

You'll want to use something like a servlet filter to catch (and handle) the exception. 您将要使用类似servlet过滤器的功能来捕获(并处理)异常。 That way, there is only one global catch block :-) 这样,只有一个全局catch块:-)

That is, you would map a filter around the FacesServlet which does: 也就是说,您将围绕FacesServlet映射一个过滤器,该过滤器可以:

try {
    chain.proceed(request, response);
} catch (Exception e) {
    // handle the exception
}

Your JSF implementation might also have the notion of an error page. 您的JSF实现可能还具有错误页面的概念。 If it does, you might have to disable the error page so that exception reaches the filter. 如果是这样,则可能必须禁用错误页面,以使异常到达过滤器。

You may find http://javaboutique.internet.com/tutorials/Servlet_Filters/ helpful. 您可能会发现http://javaboutique.internet.com/tutorials/Servlet_Filters/有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM