SimpleMappingExceptionResolver 无法解析 404

发布于 2024-12-16 14:09:59 字数 999 浏览 1 评论 0原文

下面是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羞稚 2024-12-23 14:09:59

在 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
}

Set error page in 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.

@RequestMapping(value={"/error.htm"})
    ModelAndView routToErrorHandler(HttpServletRequest request, HttpServletResponse response) {
//any logic for your themes
}
放肆 2024-12-23 14:09:59

声明可能不正确; 使用地图的属性。

<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>

另外,我不确定 SimpleMappingExceptionResolver 是否处理查找处理程序时引发的错误,而是处理从处理程序内部引发的错误。也就是说,我不确定 404 是否可以这样捕获。

如果您在 web.xml 中放置一个错误处理程序,该错误处理程序将返回到您的 servlet 中,您可以在其中以任何您喜欢的方式处理它。

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文