下面是我的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()

最佳答案

在web.xml中设置错误页面

<error-page>
    <error-code>404</error-code>
    <location>/error.html</location>
</error-page>


您的错误页面将在打开后立即重定向。

<html>
    <head>
    <title>Your Page Title</title>
    <meta http-equiv="REFRESH" content="0;url=error.htm">
    </head>
    <body>
    </body>
</html>


控制器中应该有一个请求映射,以处理error.htm请求。

@RequestMapping(value={"/error.htm"})
    ModelAndView routToErrorHandler(HttpServletRequest request, HttpServletResponse response) {
//any logic for your themes
}

10-06 07:12