简体   繁体   English

SimpleMappingExceptionResolver不解析404

[英]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.

相关问题 Spring Servlet中的404解析视图 - 404 resolving view in spring servlet 没有调用SimpleMappingExceptionResolver中的Spring doResolveException - Spring doResolveException in SimpleMappingExceptionResolver not getting called SimpleMappingExceptionResolver仅适用于某些异常 - SimpleMappingExceptionResolver only works on some Exceptions 如何从SimpleMappingExceptionResolver中排除ClientAbortException - How to exclude ClientAbortException from SimpleMappingExceptionResolver 在SimpleMappingExceptionResolver中获取应用程序上下文URL - Get Application context URL in SimpleMappingExceptionResolver 并非所有异常都在SimpleMappingExceptionResolver中映射到resolveException - Not all exceptions are mapping to resolveException in SimpleMappingExceptionResolver Spring 3 SimpleMappingExceptionResolver warnLogCategory log4j - Spring 3 SimpleMappingExceptionResolver warnLogCategory log4j 如何记录Spring的SimpleMappingExceptionResolver的异常堆栈跟踪 - How to log exception stacktrace for Spring's SimpleMappingExceptionResolver Spring MVC:使用SimpleMappingExceptionResolver,将哪些参数传递给视图 - Spring MVC: With SimpleMappingExceptionResolver, what parameters are passed to the view Spring mvc 3.1 tomcat 7 - 404不解析jsp但控制器注释工作正常 - Spring mvc 3.1 tomcat 7 - 404 not resolving jsp but controller annotations working fine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM