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

I work with tomcat 7, and I created and deployed Spring MVC webapp in tomcat 7, and it works fine. I want that whenever a 404 error occurs on my server, it should be redirected to the user page that I created in my webapp. I configured my webapp as the default webapp in tomcat.

I tried to do 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 with this.

+4
source share
3 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"; } } 
+8
source

Since you are already working in the WEB-INF folder (web.xml), you do not need to mention WEB-INF .

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

Also check out this tutorial .

+4
source

Go to the WEB-INF> Views folder.

Create a page, for example: error_404.jsp.

Now you need to create a controller.

 @RequestMapping(value="/error", method = RequestMethod.GET) public String error_404(){ return "error_404"; } 

Now edit the web.xml file and edit it as follows:

 <error-page> <error-code>404</error-code> <location>/error</location> </error-page> 
+1
source

Source: https://habr.com/ru/post/1502000/


All Articles