将 404 错误页面重定向到 Tomcat 中我的 Spring MVC webapp 的自定义页面 [英] Redirecting a 404 error page to a custom page of my Spring MVC webapp in Tomcat

查看:43
本文介绍了将 404 错误页面重定向到 Tomcat 中我的 Spring MVC webapp 的自定义页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 tomcat 7,我已经在 tomcat 7 中构建和部署了一个 Spring MVC webapp,它工作得非常好.我想要的是,每当我的服务器上发生 404 错误时,它应该被重定向到我在我的 web 应用程序中构建的自定义页面.我已将我的 webapp 配置为 tomcat 中的默认 webapp.

I am working with tomcat 7 and I've built and deployed a Spring MVC webapp in tomcat 7 and it's working perfectly fine. What I want is that, whenever a 404 error occurs on my server, it should be redirected to a custom page which I have built in my webapp. I have configured my webapp as a default webapp in tomcat.

我试过这样做:

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

但一切都是徒劳的.

很高兴有人能帮我解决这个问题.

Glad if someone can help me out in this.

推荐答案

你可以这样做:

<error-page>
    <error-code>404</error-code> 
    <location>error404</location>
</error-page>

然后:

@Controller
public class ErrorController {

    @RequestMapping("/error404")
    protected String error404() {
        return "/templates/error/error.html";
    }
}

这篇关于将 404 错误页面重定向到 Tomcat 中我的 Spring MVC webapp 的自定义页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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