开发者

Adding an attribute to a ModelAndView

开发者 https://www.devze.com 2023-03-10 18:06 出处:网络
I\'m writing a HandlerInterceptor开发者_如何学编程 that needs to insert a certain session-scoped bean into the Model. postHandle\'s signature looks like this:

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?


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(..)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号