Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Spring框架开始写入客户端后处理java异常_Spring_Spring Mvc_Exception Handling - Fatal编程技术网

在Spring框架开始写入客户端后处理java异常

在Spring框架开始写入客户端后处理java异常,spring,spring-mvc,exception-handling,Spring,Spring Mvc,Exception Handling,在我的web应用程序中,我遇到一个场景,其中一个视图出现异常。由于web服务器已经开始向客户机写入数据,它遇到异常并转到我在web.xml中定义的默认错误页面 我尝试过类似于response.reset()和response.resetBuffer()但这些都没有改变行为。发送到客户端的内容如下所示 <html> <head> <!-- Partial content delivered --> <!-- EXCEPTION OC

在我的web应用程序中,我遇到一个场景,其中一个视图出现异常。由于web服务器已经开始向客户机写入数据,它遇到异常并转到我在web.xml中定义的默认错误页面

我尝试过类似于
response.reset()
response.resetBuffer()但这些都没有改变行为。发送到客户端的内容如下所示

<html>
  <head>

    <!-- Partial content delivered -->

    <!-- EXCEPTION OCCURS, error.jsp is sent -->

<html>
  <head>...</head>
  <body>
    <!-- Stack trace -->
  </body>
</html>

...

因此,正如您所看到的,HTML正在变得完全混乱,因为异常发生在一些数据发送到客户机之后,并且没有呈现任何内容,甚至error.jsp也没有呈现。所以说清楚我的问题是。避免这种情况发生的最佳做法是什么?在我的应用程序的用户收到部分内容后,我如何重定向他们?

最佳做法是修复错误,使生成HTML标记时无法引发异常

如果您遵循MVC原则,那么可能生成异常(验证、业务调用、数据库调用)的每个任务都应该在分派到视图之前从操作中完成。视图应该只需要生成标记并从存储在请求中的bean中获取要显示的信息。除非代码中有bug,否则这永远不会引发任何异常