首先理解,model从哪里来?
SpringMVC内部使用一个org.springframework.ui.Model接口存储的数据模型,它的功能类似于Java.uitl.Map,但是比Map更好用 org.springframework.ui.ModelMap实现Map接口。
SpringMVC在调用方法前会创建一个隐含的数据模型,作为模型数据的存储容器, 成为”隐含模型”。
如果处理方法入参为Map或者Model类型,SpringMVC会将隐含模型的引用传递给这些入参。
spring Web MVC 提供Model、Map或ModelMap让我们能去暴露渲染视图需要的模型数据。
@RequestMapping(value = "/model")
public String createUser(Model model, Map model2, ModelMap model3) {
model.addAttribute("a", "a");
model2.put("b", "b");
model3.put("c", "c");
System.out.println(model == model2);
System.out.println(model2 == model3);
return "success";}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
虽然此处注入的是三个不同的类型(Model model, Map model2, ModelMap model3),但三者是同一个对象。
AnnotationMethodHandlerAdapter和RequestMappingHandlerAdapter将使用BindingAwareModelMap作为模型对象的实现,即此处我们的形参(Model model, Map model2, ModelMap model3)都是同一个BindingAwareModelMap实例。
另外关于Model和ModelAndView
@RequestMapping(value = "/mergeModel")
public ModelAndView mergeModel(Model model) {
model.addAttribute("a", "a");//①添加模型数据
ModelAndView mv = new ModelAndView("success");
mv.addObject("a", "update");//②在视图渲染之前更新③处同名模型数据
model.addAttribute("a", "new");//③修改①处同名模型数据
//视图页面的a将显示为"update" 而不是"new"
return mv;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
从代码中我们可以总结出功能处理方法的返回值中的模型数据(如ModelAndView)会 合并 功能处理方法形式参数中的模型数据(如Model),但如果两者之间有同名的,返回值中的模型数据会覆盖形式参数中的模型数据。
在处理方法的方法体中,可以使用如下方法添加数据模型:
[ModelAndView]
addObject(String attributeName,Object attributeValue);
addAllObject(Map<String,?> modelMap);
- 1
- 2
- 3
- 1
- 2
- 3
[Model]
model.addAttribute("person", person)
- 1
- 2
- 1
- 2
[Map]
put(String attributeName,Object attributeValue);
- 1
- 2
- 1
- 2
Tips:
-
放到Map中的数据如何传值到前台,以及前台如何取值?
ModelMap对象主要用于传递控制方法处理数据到结果页面,也就是说我们把结果页面上需要的数据放到ModelMap对象中即可,他的作用类似于request对象的setAttribute方法的作用,用来在一个请求过程中传递处理的数据。
① 通过以下方法向页面传递参数:
addAttribute(String key,Object value);
② JSP页面取值:
${requestScope.key} 。
总结:
Map、Model、ModelMap以及ModelAndView都是从requestScope域对象中取值,只是各个对象在后台添加数据的方法不同而已,并且Map、Model、ModelMap三者都是同一个BindingAwareModelMap实例。
后台具体代码:
/**
* 目标方法可以添加 Map 类型(实际上也可以是 Model 类型或 ModelMap 类型)的参数,
* 都是将Map、Model、ModelMap中的数据放到requestScope域对象中
* @param map
* @return
*/
@RequestMapping("/testMap")
public String testMap(Map<String,Object> map,Model model,ModelMap modelMap){
System.out.println("map="+map.getClass().getName());
System.out.println("model="+model.getClass().getName());
System.out.println("modelMap="+modelMap.getClass().getName());
model.addAttribute("model", new String("model_liuchangjie"));
modelMap.addAttribute("modelMap", new String("modelMap_liuchangjie"));
map.put("map", "liuchangjie");
return SUCCESS;
}
/**
* 目标方法的返回值可以是 ModelAndView 类型。 其中可以包含视图和模型信息 SpringMVC 会把 ModelAndView 的
* model 中数据放入到 request 域对象中.
*
* @return
*/
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
String viewName = SUCCESS;
ModelAndView modelAndView = new ModelAndView(viewName);
// new Date()作为一个匿名model
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
String newDate = sdf.format(date);
modelAndView.addObject("time", newDate);
modelAndView.addObject("aaa", "aaabbb");
return modelAndView;
}
JSP页面取数据的代码:
测试modelAndView:
<br>
<br>
requestScope time: ${requestScope.time}
<br>
requestScope aaa: ${requestScope.aaa}
<br>
测试Map、Model、ModelMap:
<br>
<br>
requestScope map: ${requestScope.map}
<br>
requestScope model: ${requestScope.model}
<br>
requestScope modelMap: ${requestScope.modelMap}