向ModelAndView添加属性 [英] Adding an attribute to a ModelAndView
问题描述
我正在编写一个HandlerInterceptor
,需要将某个会话范围的bean插入到Model
中. postHandle
的签名如下:
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
没有addAttribute
功能.如何在ModelAndView
中添加属性,以便可以在视图内部使用request.getAttribute
访问它?
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?
推荐答案
还有其他一些间接方式,通过modelAndView.getModel()
或modelAndView.getModelMap()
.但是您应该更喜欢addObject(..)
版本.实际上,它会调用getModelMap().addAttribute(..)
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(..)
这篇关于向ModelAndView添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!