Mohamed Nabli
Mohamed Nabli

Reputation: 1649

Intercept ServletException and return 404 Page not Found

I m workin on a Spring MVC based application and when I request an URL ("/kama") that doesn't exist, the following exception is thrown:

javax.servlet.ServletException: Could not resolve view with name '' in servlet with name 'dispatcher'

while I need to throw Page not found 404, here are my mapped Urls:

Mapped "{[/index],methods=[GET]}"

Mapped "{[/search],methods=[GET]}"

Mapped "{[/display],methods=[GET]}"

Mapped "{[],methods=[GET]}"

Mapped "{[/json/allProducts],methods=[GET]}"

can you show me please how to intercepte that exception and return a Page Not Found 404 error?

Upvotes: 0

Views: 275

Answers (1)

Ivan Ermolaev
Ivan Ermolaev

Reputation: 1032

Add the following in your web.xml, where error/404.html is your custom error page:

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

And your error means that you doesn't have a view, not URL. (maybe you're using Thymeleaf or jsp, or something else) You may do the following:

<error-page>
    <exception-type>java.lang.Throwable</exception-type >
    <location>/ErrorHandler</location>
</error-page>

Upvotes: 1

Related Questions