Announcement Announcement Module
No announcement yet.
@ExceptionHandler Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • @ExceptionHandler

    Hi All,

    i'm Using Spring MVC @ExceptionHandler to manage exceptions
    and i want to access the ModelMap Object used in the controller that throws the exception.

    I tried to pass it as a parameter to the handler method but it doesnt work,
    i have an error telling that the type of the argument is not resolved.

    is there a way to access the ModelMap in the Exception Handler.

    Thanks for your reply.

  • #2
    The model is tied to the request so, yes you can obtain it in the controller...just pass "ModelMap model" as an argument to your method annotated with @ExceptionHandler, and voilą...


    • #3

      Thank you for your reply .

      Actually the model map is visible in the controller in the method that is annotated with @RequestMapping by passing it as an argument
      but it doesn't work with the method that is annotated with @ExceptionHandler

      to clarify the problem here is what i ve done

      public ModelAndView verifierInfosClient(HttpServletRequest request,ModelMap mm) throws FunctionalException, TechnicalException {

      //some code that throws functional exception


      public ModelAndView handleFunctionalException(FunctionalException fx, HttpServletRequest request,Model model) {
      //some code

      and i got this exception

      java.lang.IllegalStateException: Unsupported argument [org.springframework.ui.Model] for @ExceptionHandler

      Thank you for reply.


      • #4
        Ok, my mistake...if it's not supported then you will have to use the request directly (the Model is just a key, value map representing request attributes, so having access to the request means you can retrieve model attributes with request.getAttribute(key) method).

        It's strange though...are you sure you are using latest Spring release and that you are configuring your mvc with <mvc:annotation-driven/>?


        • #5
          i'm using 3.0.3.RELEASE not the last one , and the configuration is ok its

          i'll make a test with latest relasease 3.0.4 i seen that some changes are done with @ExceptionHandler.


          • #6
            Don't bother. in the API it is clearly specified that you don't have access to the Model as attribute in methods annotated with @ExceptionHandler. Use the request.getAttribute() directly.

            This would be a nice feature to add in future releases of Spring though, since having to work with raw request is such a nuisance...


            • #7
              I can't find with which key i can retrieve the ModelMap Object. Can you provide me with the key .



              • #8
                Maybe I didn't explain myself clearly...The ModelMap object is nothing but a key, value map where the key is a String and the value is an Object. From the http protocol point of view, you only have the request and the response at your disposal; so what happens internally when you use a ModelMap in your controller is that Spring's mvc engine converts your model map into a set of standard request ATTRIBUTES. So, if in your controller you add to your model, say, a java.util.Date with today's date under the key "today", when you return from your controller method Spring will add that to the request with request.setAttribute("SOME_PRE_KEYtoday", your_date_object).

                My suggestion was to recover the objects you need and that you put in your model using request.getAttribute("SOME_PRE_KEYtoday") in your @ExceptionHandler annotated method, since you do not have your model at your disposal but you still have the request.


                • #9
                  thats clear now , i misundertstood the bahavior i thought Spring already store the ModelMap as a request Object with some key .

                  I Agree with you , it will be interresting i we can access the ModelMap object directly in the ExceptionHandler , bacause we may need some informations from the model when an exception occures and it's better than using the request object.

                  Thank you very much for your help.