5

I have a pretty big project with a lot of different controllers. I do have a main controller that has all of the mappings for the start of the program like (/login, /resetPassword, /logout, etc). I also do not have a web.xml file in the project. I need to add a custom error page for all unmapped requests. I have tried creating my own exception class and didn't work. Most of the solutions I find are to add error location to the web.xml. I would prefer not to have to create one but if anybody has any tips or can push me in the right direction that would help out so much. I've been stuck on this problem for a couple of days now. Thanks.

2
  • You can check this post and see if it works for you:[enter link description here][1] [1]: stackoverflow.com/questions/24498662/… Commented Aug 18, 2015 at 3:25
  • Thanks I had tried this solution but no luck. Thanks though! The reply at the bottom helped out a lot. Commented Aug 18, 2015 at 17:39

2 Answers 2

1

You should use 404 mapped to an error page in web.xml. Because it will handle even url requests that are not mapped to your DispatcherServlet. For example, imagine your Spring DispatcherServlet is mapped to any url ending in .htm, now some mistypes and tried to access something/somethingelse.do your application server will now present its own default error page to the user, which might not be pleasant.

The only time you should think about serving custom error pages from your MVC controllers, is when you have something specific to show the user. Specific as in, if an exception is encountered in this particular controller, I want to show a specific message, or redirect the user to a specific page. In these cases, you can use @ExceptionHandler methods in Spring controllers.

For more, look at this blog post, or refer the MVC documentation: https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this helped out a lot!
@jergamb Thanks. Happy to help. If you think that answer was satisfactory, please do accept it.
0

See this answer: How to make a filter to detect if the user requested a page that is not found?

  1. Make a Filter
  2. Use HttpServletResponseWrapper and override the sendError() and setStatus()
  3. Pass the wrapped response through chain.doFilter(req, wrapper)
  4. If you get a sendError() in your wrapper, see if it's a 404.
  5. Take appropriate response.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.