Model population in Spring MVC @ExceptionHandler

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

Or, more specifically, for my problem: my models passed to my views have some data populated with @ModelAttribute methods (for example, detailed information about the user account), and I would like to have them installed in my @ExceptionHandler .

For example, since my error view page uses the same title and menu as my other pages, I want to display the current username (and other information, such as the number of unread messages, etc.).

I know that @ExceptionHandler exists outside of @Transaction (as it should be!), So I obviously can't just (and don't want to) run some queries again. Rather, I would like to pre-populate ModelMap or ModelAndView or something else, and make sure the exception handler handles this β€” or at least the model data becomes available when rendering the view.

I hope this question makes sense, I'm pretty new to Spring MVC, so I can mix a few concepts here and there ...

+6
source share
2 answers

javadoc ExceptionHandler indicates the following with respect to arguments that can be passed to a handler method:

Handler methods that are annotated using this annotation are allowed to have very flexible signatures. They can have arguments of the following types, in random 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, eg 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, ie the configured LocaleResolver in a Servlet environment and the portal locale in a Portlet environment). 6. InputStream / Reader for access to the request content. This will be the raw InputStream/Reader as exposed by the Servlet/Portlet API. 7. OutputStream / Writer for generating the response 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, to populate the view model that you will use to display the error, you probably have to go with WebRequest

+3
source

I thought it was possible, but:

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

Important Note. A model cannot be a parameter of any @ExceptionHandler method. Instead, customize the model inside the method using ModelAndView, as shown above with handleError ().

It seems you should abandon ModelAndView, as in any other controller, so it should be created again + selection of possible values ​​from HttpServletRequest.

+1
source

Source: https://habr.com/ru/post/975612/


All Articles