Announcement Announcement Module
Collapse
No announcement yet.
ModelAttribute on exception handlers Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ModelAttribute on exception handlers

    I am new to spring MVC. So, sorry if I am saying something that does not make any sense.
    I would like to get the modelAttribute(my form object) in my exceptions handlers, is that possible?
    So, I could be able to send the previous form data to the JSP that is handling the exception.

    I read the spring MVC exception handler documentation, and it seemed not possible.
    So what you masters of Spring can tell me ?
    Is there any easy and not so forced way to do that?

    Spring framework is normally flexible and extensible. But the exception handler life cycle in Spring MVC has being a little bit hard to work with, at least for me .
    Last edited by rafaelweingartner; Jun 6th, 2012, 09:28 PM.

  • #2
    Well it depends... If you use an annotated method as an exception handler then you can (@ExceptionHandler) but if you use the HandlerExceptionResolver interface you cannot. The exception handling mechanism is broader then just controllers and as such it has to cover the common cases and not every handler knows or has a model and as such that isn't available, next to that the error could occur even before the model would be constructed or would be there.

    However it doesn't mean the data isn't there, it still is http and as such everything is available as either request parameters or in the session (if you use @SessionAttributes).

    Comment


    • #3
      I know that this is an old post but I am having the same problem right now.

      I need to keep the same JSP when a business exception is thrown but the model is not keeping the controller values.

      Does anyone have a solution for this?

      Thanks

      Comment


      • #4
        When I had the problem, I looked at the source code, and the model will not keep the controller values.
        ​So, I created a workaroud, an abstractController that every controller extends and implemented an InitBinder method. So, I would have the opportunity to save the data that was received on the request. Then, every single controller that I created would extend this abstract controller.
        Code:
        package br.com.myProject.spring.mvc.controller;
        
        import javax.servlet.http.HttpServletRequest;
        
        import org.apache.commons.lang3.text.WordUtils;
        import org.apache.commons.logging.Log;
        import org.apache.commons.logging.LogFactory;
        import org.springframework.validation.BindingResult;
        import org.springframework.web.bind.WebDataBinder;
        import org.springframework.web.bind.annotation.InitBinder;
        
        public abstract class MyProjectAbstractController {
        
            public static final String MyProjectBindingResultRequestKey = "BindingResultObject";
            
            protected final Log logger = LogFactory.getLog(getClass());
        
            @InitBinder
            public void initBinder(HttpServletRequest request, WebDataBinder dataBinder){
                Object modelObject = dataBinder.getTarget();
                if(modelObject == null){
                    this.logger.debug("There is no modelObject assigned for this request.");
                    return;
                }
                String formObjectName = WordUtils.uncapitalize(modelObject.getClass().getSimpleName());
                request.setAttribute(formObjectName, modelObject);
                
                BindingResult bindingResult = dataBinder.getBindingResult();
                if(bindingResult !=null){
                    this.logger.warn(String.format("There is no binding result object for this request. ModelObjectName[%s], request[%s]", formObjectName, request.getServletPath()));
                    String springMvcBindingResultRequestKey = BindingResult.MODEL_KEY_PREFIX + formObjectName;
                    request.setAttribute(springMvcBindingResultRequestKey, bindingResult);
                    request.setAttribute(MyProjectBindingResultRequestKey, bindingResult);
                }
            }
        }

        Comment

        Working...
        X