Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting a 404 error page to a custom page of my Spring MVC webapp in Tomcat

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.

I have tried doing this:

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

But all in vain.

Glad if someone can help me out in this.

like image 227
Hitesh Avatar asked Dec 11 '22 11:12

Hitesh


1 Answers

You can do something like this:

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

And then:

@Controller
public class ErrorController {

    @RequestMapping("/error404")
    protected String error404() {
        return "/templates/error/error.html";
    }
}
like image 121
andvicoso Avatar answered Apr 13 '23 01:04

andvicoso