将属性添加到ModelAndView

Pie*_*ter 7 spring spring-mvc

我正在编写一个HandlerInterceptor需要在其中插入一个会话范围的bean Model.postHandle签名看起来像这样:

public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception
Run Code Online (Sandbox Code Playgroud)

ModelAndView没有任何addAttribute功能.如何在a中添加属性ModelAndView以便我可以request.getAttribute在视图中访问它?

Boz*_*zho 16

使用 modelAndView.addObject("key", value)

还有一些其他间接方式,通过modelAndView.getModel()modelAndView.getModelMap().但你应该更喜欢这个addObject(..)版本.事实上,它调用getModelMap().addAttribute(..)