SimpleMappingExceptionResolver 无法解析 404
下面是我的 spring 配置文件:
<bean class="com.web.handler.CustomSimpleMappingExceptionResolver" >
<property name="exceptionMappings">
<props>
<prop key="java.lang.Throwable">error</prop>
</props>
</property>
</bean>
类 CustomSimpleMappingExceptionResolver
public class CustomSimpleMappingExceptionResolver extends SimpleMappingExceptionResolver{
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) {
if(int a = 1)
return new ModelAndView("ViewName1");
else
return new ModelAndView("ViewName2");
}
我的 web.xml
没有错误页面。我希望根据我的逻辑在 resolveException()
中显示不同的视图。
在 CustomSimpleMappingExceptionResolver
中,如果出现 404,则不会调用 resolveException()
类。
Below is my spring configuration file:
<bean class="com.web.handler.CustomSimpleMappingExceptionResolver" >
<property name="exceptionMappings">
<props>
<prop key="java.lang.Throwable">error</prop>
</props>
</property>
</bean>
Class CustomSimpleMappingExceptionResolver
public class CustomSimpleMappingExceptionResolver extends SimpleMappingExceptionResolver{
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) {
if(int a = 1)
return new ModelAndView("ViewName1");
else
return new ModelAndView("ViewName2");
}
My web.xml
has no error page. I am looking to show different view according to my logic in resolveException()
.
In CustomSimpleMappingExceptionResolver
class resolveException()
is not being called in case of 404.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 web.xml 中设置错误页面,
错误页面将在打开后立即重定向。
您的控制器中应该有一个请求映射来处理 error.htm 请求。
Set error page in web.xml
your error page will redirect as soon as it opened.
There should be a request mapping in your controller to handle error.htm request.
声明可能不正确; 使用地图的属性。
另外,我不确定 SimpleMappingExceptionResolver 是否处理查找处理程序时引发的错误,而是处理从处理程序内部引发的错误。也就是说,我不确定 404 是否可以这样捕获。
如果您在 web.xml 中放置一个错误处理程序,该错误处理程序将返回到您的 servlet 中,您可以在其中以任何您喜欢的方式处理它。
The declaration might be incorrect; use a map instead of properties.
Also, I'm not sure SimpleMappingExceptionResolver handles errors thrown when finding a handler but rather it handles errors thrown from inside handlers. That said, I'm not sure 404 can caught this way.
If you put a error handler in web.xml that will go back into your servlet where you can handle it any way you like.