3

How can I implement custom http error handling in spring mvc.

For example: I have a url http://localhost:8080/demo/canvas

It display my canvas page but if the user missed the url and typed http://localhost:8080/demo/canva

It shows me Tomcat Rendered HTTP Status 404. I want it to be custom jsp page.

2 Answers2

2

In your web.xml you can provide a custom error page mapping based upon the error code:

<error-page>
    <error-code>404</error-code>
    <location>/PageNotFoundError.jsp</location>
</error-page>

This jsp file will be present parallel to WEB-INF folder.

Ravi Ranjan
  • 740
  • 2
  • 10
  • 31
  • Hope this will help for Spring specific 404 error page redirects: http://stackoverflow.com/questions/21061638/spring-mvc-how-to-return-custom-404-errorpages – Ravi Ranjan Feb 23 '16 at 10:12
  • 1
    ok i resolved it. The mapping in web.xml was conflicting with the spring exception configuration in dispatcher-servlet.xml. TThank's Ravi – user3506421 Feb 23 '16 at 10:35
1

Working in my application,, try this

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

Note: Make sure u restart server each time wen u make changes in web.xml

Santosh Jadi
  • 1,479
  • 6
  • 29
  • 55