我想处理异常,以便 URL 信息自动显示给客户端。是否有捷径可寻?
<bean id="outboundExceptionAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
<!-- what property to set here? -->
</bean>
你有两个选择:
Spring参考15.9.1 HandlerExceptionResolver http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#d0e29676
Spring HandlerExceptionResolvers 缓解意外的痛苦
控制器处理您的请求时发生的异常
符合要求。 HandlerExceptionResolvers 有点类似于
您可以在 Web 应用程序中定义的异常映射
描述符 web.xml。然而,它们提供了一种更灵活的方式
处理异常。他们提供有关哪个处理程序的信息
抛出异常时执行。此外,一个程序化的
处理异常的方式为您提供了更多的响应选择
在将请求转发到另一个 URL 之前适当地进行操作(相同
最终结果与使用 servlet 特定异常映射时相同)。
The 处理异常解析器 http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/web/servlet/HandlerExceptionResolver.html有一种方法,包含您需要的一切:
HandlerExceptionResolver.resolveException(HttpServletRequest request,
HttpServletResponse response,
Object handler, Exception ex)
或者,如果您需要针对不同控制器的不同处理程序:Spring参考第15.9.2章@ExceptionHandler http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-exceptionhandler
@ExceptionHandler(IOException.class)
public String handleIOException(IOException ex, HttpServletRequest request) {
return "every thing you asked for: " + request;
}
短问短答
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)