向ModelAndView添加属性 [英] Adding an attribute to a ModelAndView

查看:84
本文介绍了向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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆