如何配置Spring HandlerExceptionResolver来处理在jsp中抛出的NullPointerException? [英] How to configure spring HandlerExceptionResolver to handle NullPointerException thrown in jsp?

查看:156
本文介绍了如何配置Spring HandlerExceptionResolver来处理在jsp中抛出的NullPointerException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从jsp抛出一个NullPointerException,例如使用<%null.toString(); %>

From a jsp is thrown a NullPointerException for example using <% null.toString(); %>

此异常不由HandlerExceptionResolver处理,但被抛出到Web容器(tomcat)并转换为代码500错误

This exception is not handled by the HandlerExceptionResolver, but thrown to the web container(tomcat) and converted into a code 500 error.

如何配置spring以在HandlerExceptionResolver中获取该错误?

How can I configure spring to get that error in my HandlerExceptionResolver ?

详细信息:


  • 可以将Spring配置为处理控制器内部抛出的异常,但不能视图抛出异常。 / li>
  • 当然我可以解决NullPointerException,但是我想设计一个可以正常解决Web应用程序中可能出现的问题的解决方案,以便向用户显示一个用户友好的消息。 li>
  • Spring can be configured to handle exceptions thrown inside Controllers, but not exceptions thrown by view.
  • Of course i can resolve the NullPointerException, but i want to design a solution that will gracefully resolve any possible problem on the web application in order to display a user friendly message to the user.

推荐答案

请参阅 HandlerInterceptor 接口。您将需要afterCompletion方法。然后,您可以拦截响应,然后设置相应的头信息以重定向到容器配置的错误网页。你是对的,Spring没有这个功能,这必须由web.xml指定,该web.xml确定哪些代码映射到哪些页面。

See the HandlerInterceptor interface instead. You'll want the afterCompletion method. You can then intercept the response and then set the appropriate header information to redirect to a container-configured error web page. You're right that Spring doesn't have this functionality, this is going to have to be specified by the web.xml which determines which codes map to which pages.

这篇关于如何配置Spring HandlerExceptionResolver来处理在jsp中抛出的NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆