将 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";
    }
}