Spring MVC中ModelAndView

ModelAndView是springframework的一个类。要想使用ModelAndView要引入org.springframework.web.servlet。

ModelAndView包含了一个view和model的对象。其中model是map的一个实现类的子类,视图解析器将model的每个元素都通过request.setAttribute(name,value)添加到request的请求域中,jsp通过EL表达式获取到值。

ModelAndView添加数据

(1)通过ModelAndView内部方

public ModelAndView addObject(String attributeName, Object attributeValue

(2)既然ModelAndView是map的一个子类,那么也可以通过map的方式来添加数据

mav.getModel().put("name", "caoyc");

官方文档:https://docs.spring.io/spring-framework/docs/5.0.3.RELEASE/javadoc-api/org/springframework/web/servlet/ModelAndView.html

原文地址:https://www.cnblogs.com/NaCl/p/9769395.html