[英]SimpleMappingExceptionResolver not Resolving 404
Below is my spring configuration file: 下面是我的spring配置文件:
<bean class="com.web.handler.CustomSimpleMappingExceptionResolver" >
<property name="exceptionMappings">
<props>
<prop key="java.lang.Throwable">error</prop>
</props>
</property>
</bean>
Class CustomSimpleMappingExceptionResolver
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. 我的
web.xml
没有错误页面。 I am looking to show different view according to my logic in resolveException()
. 我希望根据我在
resolveException()
逻辑显示不同的视图。
In CustomSimpleMappingExceptionResolver
class resolveException()
is not being called in case of 404. 在
CustomSimpleMappingExceptionResolver
类中,在404的情况下不调用resolveException()
。
The declaration might be incorrect; 声明可能不正确; use a map instead of properties.
使用地图而不是属性。
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<map>
<entry key="DataAccessException" value="data-error" />
<entry key="com.stuff.MyAppRuntimeException" value="app-unchecked-error" />
<entry key="com.stuff.MyAppCheckedException" value="app-checked-error" />
</map>
</property>
<property name="defaultErrorView" value="general-error"/>
</bean>
Also, I'm not sure SimpleMappingExceptionResolver handles errors thrown when finding a handler but rather it handles errors thrown from inside handlers. 此外,我不确定SimpleMappingExceptionResolver处理在查找处理程序时抛出的错误,而是处理从内部处理程序抛出的错误。 That said, I'm not sure 404 can caught this way.
也就是说,我不确定404能否抓住这种方式。
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. 如果你在web.xml中放入一个错误处理程序,它将返回到你的servlet,你可以按照自己喜欢的方式处理它。
Set error page in web.xml 在web.xml中设置错误页面
<error-page>
<error-code>404</error-code>
<location>/error.html</location>
</error-page>
your error page will redirect as soon as it opened. 您的错误页面将在打开后立即重定向。
<html>
<head>
<title>Your Page Title</title>
<meta http-equiv="REFRESH" content="0;url=error.htm">
</head>
<body>
</body>
</html>
There should be a request mapping in your controller to handle error.htm request. 控制器中应该有一个请求映射来处理error.htm请求。
@RequestMapping(value={"/error.htm"})
ModelAndView routToErrorHandler(HttpServletRequest request, HttpServletResponse response) {
//any logic for your themes
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.