Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model population in Spring MVC @ExceptionHandler

Is there a way to let my @ExceptionHandler methods have access to the model attributes populated by the @RequestMapping method that triggered the exception in question?

Or, more specifically for my problem: my models passed to my views have some data populated from @ModelAttribute (such as detailed information on the user account) methods, and I'd like those to be also set in my @ExceptionHandler methods.

For instance, since my error view page uses the same header and menu as my other pages, I want to show the current user name (and other info, such as number of unread messages etc).

I know the @ExceptionHandler exists outside the @Transaction (as it should!), so I obviously can't just (and don't want to) run some queries again. Rather, I'd like to pre-populate the ModelMap or ModelAndView or whatever, and make sure the exception handler gets hold of that - or at least that the model data is made available when rendering the view.

I hope this question makes sense, I'm fairly new to Spring MVC so I may be mixing a few concepts here and there...

like image 948
Wouter Lievens Avatar asked Sep 21 '14 19:09

Wouter Lievens


2 Answers

The javadoc of ExceptionHandler states the following with regard to the arguments that can be passed to the handler method:

Handler methods which are annotated with this annotation are allowed to have very flexible signatures. They may have arguments of the following types, in arbitrary order:

1. An exception argument: declared as a general Exception or as a more specific exception. This also serves as a mapping hint if the annotation itself does not narrow the exception types through its value().
2. Request and/or response objects (Servlet API or Portlet API). You may choose any specific request/response type, e.g. ServletRequest / HttpServletRequest or PortletRequest / ActionRequest / RenderRequest. Note that in the Portlet case, an explicitly declared action/render argument is also used for mapping specific request types onto a handler method (in case of no other information given that differentiates between action and render requests).
3. Session object (Servlet API or Portlet API): either HttpSession or PortletSession. An argument of this type will enforce the presence of a corresponding session. As a consequence, such an argument will never be null. Note that session access may not be thread-safe, in particular in a Servlet environment: Consider switching the "synchronizeOnSession" flag to "true" if multiple requests are allowed to access a session concurrently.
4. WebRequest or NativeWebRequest. Allows for generic request parameter access as well as request/session attribute access, without ties to the native Servlet/Portlet API.
5. Locale for the current request locale (determined by the most specific locale resolver available, i.e. the configured LocaleResolver in a Servlet environment and the portal locale in a Portlet environment).
6. InputStream / Reader for access to the request's content. This will be the raw InputStream/Reader as exposed by the Servlet/Portlet API.
7. OutputStream / Writer for generating the response's content. This will be the raw OutputStream/Writer as exposed by the Servlet/Portlet API. 
8. Model as an alternative to returning a model map from the handler method. Note that the provided model is not pre-populated with regular model attributes and therefore always empty, as a convenience for preparing the model for an exception-specific view.

So in order to populate the model of the view you will be using to display the error, you'll probably have to go with WebRequest

like image 78
geoand Avatar answered Nov 15 '22 17:11

geoand


Thought its possible, but:

http://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

Important Note: the Model may not be a parameter of any @ExceptionHandler method. Instead, setup a model inside the method using a ModelAndView as shown by handleError() above.

Seems you have to cant pass ModelAndView as in any other controller, so it has to be built once again + fetch possible values from HttpServletRequest.

like image 25
hi_my_name_is Avatar answered Nov 15 '22 16:11

hi_my_name_is