如何将异常发送到ExceptionController?
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="mappedHandlers">
<set>
<ref bean="exceptionController" />
</set>
</property>
<property name="defaultErrorView" value="tiles/content/error" />
</bean>
我正在尝试将异常发送到控制器,以便我可以创建重定向。如果我注释掉mappedHandlers 部分,则会显示错误图块,但它只是一个图块。其余的图块正常加载。我需要在控制器中进行重定向,以便可以显示错误页面而不仅仅是错误图块。
我找不到足够的信息或示例如何调用异常控制器中的某些方法。
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="mappedHandlers">
<set>
<ref bean="exceptionController" />
</set>
</property>
<property name="defaultErrorView" value="tiles/content/error" />
</bean>
I'm trying to send exceptions to a controller so I can create a redirect. If I comment out the mappedHandlers part then the error tile is displayed but it is only a tile. The rest of the tiles load normally. I need to make a redirect in the controller so I can show an error page not just an error tile.
I can't find enough information or an example how the exception invokes some method in exceptionController.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您误解了
mappedHandlers
属性的用途。这就是说,这个异常解析器 bean 应该只应用于该属性中列出的控制器抛出的异常。它不会将异常发送到该控制器。如果您想发送一个简单的重定向,那么您可以执行以下操作:
但是,通过执行此操作,您将丢失有关异常的所有信息。
如果您想编写自定义代码来处理异常,那么我建议您编写自己的 HandlerExceptionResolver 实现(可能是 AbstractHandlerExceptionResolver 的子类),并使用它来代替
SimpleMappingExceptionResolver。
另一种选择是使用 @ExceptionHandler 注释样式(请参阅文档 此处)。
You're misunderstanding what the
mappedHandlers
property is for. This is saying that this exception resolver bean should only apply to exceptions thrown by the controllers listed in that property. It does not send the exceptions to that controller.If you want to send a simple redirect, then you can do somrthing like this:
You will lose all information on the exception by doing this, though.
If you want to write custom code to handle exceptions, then I suggest writing your own implementation of
HandlerExceptionResolver
(probably a subclass ofAbstractHandlerExceptionResolver
), and using that instead ofSimpleMappingExceptionResolver
.Another alternative is to use the
@ExceptionHandler
annotation style (see docs here).