在Spring中处理404错误? [英] Handle 404 errors in Spring?

查看:185
本文介绍了在Spring中处理404错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我将未映射的请求重定向到404页的代码

Here is the code I have for redirecting unmapped requests to 404 page

@RequestMapping("/**")
    public ModelAndView redirect() {

    ModelAndView mv = new ModelAndView();
    mv.setViewName("errorPage");        
        return mv;
    }

上面的代码工作正常,但问题出在css和js文件之类的网络资源上 还要进入此重定向方法,它不会加载任何文件.但是我的调度程序servlet中已经有此代码,但是spring controller无法识别此资源映射.

The above code works fine but the problem is with the web resources like css and js files also go inside this redirect method and it is not loading any of the files. But i already have this code in my dispatcher servlet,but spring controller is not recognizing this resources mapping.

<mvc:resources mapping="/resources/**" location="/WEB-INF/web-resources/" />

所以我尝试在请求映射中使用一些 regex表达式来否定资源url之类的东西

so i tried some regex expression in the request mapping to negate the resources url something like this

@RequestMapping("/{^(?!.*resources/**)}**")
    public ModelAndView redirect() {

    ModelAndView mv = new ModelAndView();
    mv.setViewName("errorPage");        
        return mv;
    }

但这不能按预期工作..因此,如果有人可以帮助,那就太好了:)

But this is not working as expected ..So if anyone could help it would be great :)

推荐答案

我找到了处理404(未映射链接)的解决方案,我使用了SimpleUrlHandlerMapping来做到这一点.

I found the solution to handle 404 (Unmapped links), I used a SimpleUrlHandlerMapping to do this.

我在我的 dispatcher servlet .xml

<!-- Here all your resources like css,js will be mapped first -->
    <mvc:resources mapping="/resources/**" location="/WEB-INF/web-resources/" /> 
    <context:annotation-config />

<!-- Next is your request mappings from controllers -->
    <context:component-scan base-package="com.xyz" /> 
    <mvc:annotation-driven />

<!-- Atlast your error mapping -->
    <bean id="errorUrlBean" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
       <property name="mappings">
        <props>
           <prop key="/**">errorController</prop>
         </props>
       </property>
    </bean>

    <bean id="errorController" class="com.xyz.controller.ErrorController">

    </bean>

com.xyz.controller.ErrorController类

public class ErrorController extends AbstractController  {


    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {
        // TODO Auto-generated method stub

        ModelAndView mv = new ModelAndView();
        mv.setViewName("errorPage");        
        return mv;
    }

}

我发现了以下原因

@RequestMapping("/**")使用 RequestHandlerMapping

<mvc:resources mapping="/resources/**" location="/WEB-INF/web-resources/" /> 

使用 SimpleUrlHandlerMapping

RequestHandlerMapping优先于SimpleUrlHandlerMapping,因此在我的情况下,这就是所有资源请求都进入重定向方法的原因.

RequestHandlerMapping takes presedence over SimpleUrlHandlerMapping, so that was the reason all resources request went inside the redirect method in my case.

因此,我只是通过在我的dipatcher servlet中将其配置为上面提供的bean并将其映射到最后,将@RequestMapping("/**")请求更改为SimpleUrlHandlerMapping,从而解决了问题.

So i just changed the @RequestMapping("/**") request to SimpleUrlHandlerMapping by configuring that as a bean as given above in my dipatcher servlet and mapped it at the last and it solved the problem.

还将以下代码添加到您的 web.xml

Also add the below code to your web.xml

  <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/jsp/error.jsp</location>
  </error-page>

现在,可以使用这种简单的解决方案将所有未映射的请求重定向,即404错误到错误页面:)

Now this simple solution can be used to redirect all unmapped requests i.e) 404 errors to an error page :)

这篇关于在Spring中处理404错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆