Map,Model,ModelMap使用方法

1.Spring MVC在内部使用了一个org.springframework.ui.Model接口存储模型数据

具体步骤:

1.Spring MVC在调用方法前会创建一个隐含的模型对象作为模型数据的存储容器。

2.如果方法的入参为Map,Model类型,Spring MVC会将隐含模型的引用传递给这些入参。在方法体内,开发者可以通过这个入参对象访问到模型中的所以数据,也可以向模型中添加新的属性数据。

@RequestMapping("/testmap")
	public String testmap(Map<String,Object> map) {
		map.put("age", 13);
		return "success";
	}
	@RequestMapping("/testModel")
	public String testModel(Model model) {
		model.addAttribute("email","[email protected]");
		return "success";
	}
	@RequestMapping("/testModelmap")
	public String testModelmap(ModelMap modelMap) {
		modelMap.addAttribute("city", "Beijing");
		return "success";
	}
${requestScope.age }
${requestScope.email}
${requestScope.city}

三种方式处理模型数据:

1.Map.put(string,object);

2.Model.AddAttribute(string,object);

3.ModelMap.AddAttribute(string,object);

猜你喜欢

转载自my.oschina.net/u/3829307/blog/1800072
今日推荐