Announcement Announcement Module
No announcement yet.
Is it possible to add an object to all my controller's ModelAndView? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is it possible to add an object to all my controller's ModelAndView?

    I create an object in an interceptor and add it to the request attributes collection.

    I need access to this object in my controller and view, currently in each and every controller action I am adding it.

    Is it possible to do this globally somehow at the interceptor level so I can have access to it? Or is manually adding it the only way?

          ModelAndView mav = new ModelAndView(...);
          mav.addObject("myObject", request.getAttribute("myObject"));
          return mav;

  • #2
    Do you want save some value or object in the interceptor and later let to any controller to access such object?


    • #3
      hi hello just new here in site..

      i think so..


      • #4
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;

        import org.springframework.web.servlet.ModelAndView;
        import org.springframework.web.servlet.handler.HandlerInt erceptorAdapter;

        public class CoreInterceptor extends HandlerInterceptorAdapter {

        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;

        public void postHandle(
        HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
        throws Exception {

        if ( modelAndView != null ) modelAndView.addObject(...);

        // It should then be available in all your views where the action passes on a modelAndView ( returns normally )


        public void afterCompletion(
        HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
        throws Exception {



        • #5
          If an exception occurs in any of the controllers, then postHandler will not be called. I have an ExceptionResolver which catches the exception, but its output won't pass through the postHnadlers. Is there some generic way in which an object can be added to a ModelAndView object before rendering, irrespective of the fact if an exception was raised or some other havoc was caused. Also if a custom jsp page is defined for handling server errors like 404 or 500, then too that object should be added.

          My use case is that each jsp page consists of a header which takes a few values from an object, which I currently add via postHandlers. The issue is that when an exception is raised, the exception bubbles to ExceptionResolver and the postHandler is not called, hence my header display gets corrupted. Also currently I am using a hack for handling the page not found scenarios by having a dummy method in a controller which maps to "/**", so that not defined pages which are served my custom page not found, also have the object added to ModelAndView via the postHandlers. Can someone suggest a cleaner way.