简体   繁体   English

您如何处理/记录Java MVC Web应用程序中的错误?

[英]How do you handle/log errors in a Java MVC web application?

I'm currently using a very simple MVC framework, Bear Bibeault's Front Man , which, for those not familiar, is pretty similar to Spring MVC (in concept at least). 我目前正在使用一个非常简单的MVC框架Bear Bibeault的Front Man ,对于不熟悉的 ,它与Spring MVC非常相似(至少在概念上)。 One of the issues I'm having is how to handle exceptions properly. 我遇到的问题之一是如何正确处理异常。

I am currently doing something like this, 我目前正在做这样的事情,

try {
    //do something
}catch (Exception ex) {
    logger.error("Logging error", ex);
    Map model = new HashMap();
    model.put("error", ex.getLocalizedMessage());
    cc.setScopedVariable("model", model);
    cc.forwardToView(ERROR_VIEW);
}

Essentially I log the exception and then forward to an error view page. 本质上,我记录了异常,然后转发到错误视图页面。

However this strikes me as not being the right way to do this. 但是,这让我感到震惊,因为它不是正确的方法。 It results in a lot of boilerplate code that isn't very DRY. 这会导致很多样板代码不是很干。

What is a better way to handle/log exceptions in a web application? 在Web应用程序中处理/记录异常的更好方法是什么?

You could do all of your logging inside an error JSP file . 您可以在错误的JSP文件中进行所有日志记录。

Add this to your web.xml file. 将此添加到您的web.xml文件。

<error-page> 
<exception-type>java.lang.Throwable</exception-type> 
<location>/error.jsp</location> 
</error-page>

In your jsp file add 在您的jsp文件中添加

<%@page isErrorPage="true" %>

Then you can place your logging inside <% %> and display the error at the same time. 然后,您可以将日志记录放入<% %>并同时显示错误。 All of your logging logic would be in one place and your code would begin to look cleaner. 您所有的日志记录逻辑都将放在一个地方,并且您的代码将开始看起来更简洁。


Instead of using a scriptlet in your JSP file, you could have the location point to a servlet. 您可以将位置指向servlet,而不是在JSP文件中使用scriptlet。 The servlet could handle all of your processing then forward to the JSP page. Servlet可以处理您的所有处理,然后转发到JSP页面。

You can also use this technique for different exceptions, so you could process the exceptions differently. 您还可以将这种技术用于不同的异常,因此可以对异常进行不同的处理。

There are varying kinds of exceptions so you should first identify how you want to show them to the user. 异常的种类繁多,因此您首先应该确定要如何向用户显示它们。 Forwarding them to a page to display an error may not be the best choice for all of the errors they encounter also keep in mind displaying an error without context can be confusing to some end users. 将它们转发到页面以显示错误可能不是它们遇到的所有错误的最佳选择,并且切记在没有上下文的情况下显示错误可能会使某些最终用户感到困惑。

One way you could handle it would be log all of the none critical errors and have a location on the website where a user could access and review them. 处理该错误的一种方法是记录所有非关键错误,并在网站上有一个用户可以访问和查看它们的位置。 (I worked on a project where we had something similar to this. The user could review warnings and errors that wouldn't prevent the application from proceeding.) (我在一个项目中进行了类似的工作。用户可以查看不会阻止应用程序继续执行的警告和错误。)

You could return them to the page in which the error occurred and populate an error message there. 您可以将它们返回到发生错误的页面,然后在其中填充错误消息。 AJAX/Comet could be used to display real time errors to the user without taking them away from the page where the error occurred. AJAX / Comet可用于向用户显示实时错误,而无需将其从发生错误的页面移开。

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

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