2

It seems that when my JSP encounters an error like a misspelled variable name (for instance), the output of the page just stops.

I have a controller that extends BaseController with functions that use @RequestMapping() annotations. I can catch exceptions in these but once any of my functions return I'm not exactly sure how this is handled.

Just looking for some insight into how this process works. I'm relatively new to Spring.

Matthew
  • 8,183
  • 10
  • 37
  • 65

1 Answers1

3

The JSP is the view - it exists to render your ouput. It shouldn't handle your programming errors, you need to fix thos beforehand. Likewise it shouldn't handle exceptions arising from problems with the business logic - those should be caught and the appropriate view displayed (e.g. an error page).

See this question for a little more on that:

How to Properly Handle Exceptions in a JSP/Servlet App?

If you still really want Spring to handle the exception in a JSP, see this question:

How to configure spring HandlerExceptionResolver to handle NullPointerException thrown in jsp?

Community
  • 1
  • 1
Paul
  • 19,704
  • 14
  • 78
  • 96
  • Am I just confused about where to handle this? From the second link you posted: "Spring can be configured to handle exceptions thrown inside Controllers, but not exceptions thrown by (the) view." Should I be looking into how to handle JSP errors instead? – Matthew Feb 04 '12 at 22:10
  • The statement you quoted is from the question. Don't rely on questions for facts...they're questions because the person doesn't understand something :) Read the answer by MetroidFan2002. In general, though, your view JSP should not throw any exceptions. All it has to do is display the data. The data should be checked before sending it to the view. You shouldn't be doing any business logic in your view. – Paul Feb 04 '12 at 22:14
  • MetroidFan2002 agrees with the poster on that though. I guess this is one of the features of MVC where, like you say the view should just be displaying data... even if that data is from an exception caught in the controller. I just end up having a hard time when my output stops and I don't have much of an idea from the JSP as to why. – Matthew Feb 04 '12 at 22:32
  • He agrees that the `HandlerExceptionResolver` won't catch the exception. The method `org.springframework.web.servlet.HandlerInterceptor.afterCompletion` is a "Callback after completion of request processing, that is, after rendering the view." Aren't you seeing the stack trace on the screen? – Paul Feb 04 '12 at 23:47
  • 1
    No stack trace in the browser - output just stops and not necessarily where the error actually happened. My thinking was that I could extend something and catch/display the exception as nice JSP errors - at the very least for debugging. – Matthew Feb 05 '12 at 00:19
  • Even the simplest syntax error in the JSP (not even business) logic will yield a blank page with no server-side explanation. – Paul Cunningham May 19 '16 at 19:50