2

I use ModelAndView objects as all people do:

ModelAndView result = new ModelAndView("view");
result.addObject("requests", requestsService.getListByBackoffice(filter, page, Config.RECORDS_PER_PAGE));

But I noticed that I have a couple of objects that I use always in most of the views.. So the question is - is there any solutions to create some kind of default assigned objects that are passed to view automatically?

Thank you

2
  • Either a servlet Filter or a Spring HandlerInterceptor. Commented Jul 26, 2013 at 15:02
  • @SotiriosDelimanolis can you provide an example or link to it? Commented Jul 26, 2013 at 15:05

2 Answers 2

4

You can register a HandlerInterceptor with your DispatcherServlet. You then implement the postHandle() method:

public class CustomInterceptor extends HandlerInterceptorAdapter /* which implements HandlerInterceptor */ {

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        modelAndView.addObject("myObject", new Object());
        // add as many as you wish                
    }

}

NOTE: The ModelAndView object may be null. This may occur if your handler method was writing to the response body directly, for example with @ResponseBody.

Depending on the url pattern you used when registering the Interceptor, the postHandle() will get called and populate your model with any objects you want.

You can also register a servlet Filter (in web.xml or WebApplicationInitializer). The filter simply adds the request attributes before dispatching to the servlet.

public class CustomFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setAttribute("myObject", new Object());
        chain.doFilter(request, response);
    }
    // ... init and destroy methods
}

NOTE: At some point during the request lifecycle, Spring adds all attributes in the model to your request attributes.

The disadvantage here is that you add the attributes whether or not your @Controller worked, as the Filter is called before Spring's DispatcherServlet. Also, the Filter is managed by your servlet container (workarounds exist) and therefore it's difficult to inject Spring beans into it.

Sign up to request clarification or add additional context in comments.

1 Comment

Interceptor registration guide here: baeldung.com/spring-mvc-handlerinterceptor
0

first solution:

I have not tried such but can do like creating ModelAndView object in Constructor or somewhere which you call always, set object which you always want to pass as default there only. call setViewName() in respective methods and add respective objects which you want to set.

second solution:

write one method which is adding that default object and call that method wherever you need (nothing but what interceptor do).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.