Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an attribute to a ModelAndView

I'm writing a HandlerInterceptor that needs to insert a certain session-scoped bean into the Model. postHandle's signature looks like this:

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

ModelAndView has no addAttribute function. How can I add an attribute to a ModelAndView so that I can access it with request.getAttribute inside my views?

like image 745
Pieter Avatar asked Jun 07 '11 08:06

Pieter


1 Answers

Use modelAndView.addObject("key", value)

There are also some other indirect ways, through modelAndView.getModel() or modelAndView.getModelMap(). But you should prefer the addObject(..) version. In fact it invokes getModelMap().addAttribute(..)

like image 174
Bozho Avatar answered Sep 21 '22 02:09

Bozho