目录
一 使用ServletAPI向request域对象共享数据
二 使用ModelAndView向request域对象共享数据
1 新建TestScopeController
2 index.html
3 书写TestScopeController
4 success.html
5 测试
三 使用Model向request域对象共享数据
1 index.html
2 TestScopeController
四 使用map向request域对象共享数据
1 . index.html
2 TestScopeController
3 测试
五 使用ModelMap向request域对象共享数据
1 . index.html
2 TestScopeController
3 测试
六 Model、ModelMap、Map的关系
七 向session域共享数据
八 向application域共享数据
1 . index.html
2 TestScopeController
3 .success.html
4 测试
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope", "hello,servletAPI");
return "success";
}

测试通过ModelAndView向请求域共享数据

/*
* * 向域对象共享数据:* 1、通过ModelAndView向请求域共享数据* 使用ModelAndView时,可以使用其Model功能向请求域共享数据* 使用View功能设置逻辑视图,但是控制器方法一定要将ModelAndView作为方法的返回值
* */@Controller
public class TestScopeController {@RequestMapping("/test/mav")public ModelAndView testMAV(){/*** ModelAndView包含Model和View的功能* Model:向请求域中共享数据* View:设置逻辑视图实现页面跳转*/ModelAndView mav = new ModelAndView();//向请求域中共享数据mav.addObject("testRequestScope", "hello,ModelAndView");//设置逻辑视图mav.setViewName("success");return mav;}
}

首页
success.html


总结 :
向域对象共享数据:1、通过ModelAndView向请求域共享数据使用ModelAndView时,可以使用其Model功能向请求域共享数据使用View功能设置逻辑视图,但是控制器方法一定要将ModelAndView作为方法的返回值
测试通过Model向请求域共享数据


/* 2、使用Model向请求域共享数据 */@RequestMapping("/test/model")public String testModel(Model model){//org.springframework.validation.support.BindingAwareModelMapSystem.out.println(model.getClass().getName());model.addAttribute("testRequestScope", "hello,Model");return "success";}
3 测试


测试通过map向请求域共享数据

/*4、使用map向请求域共享数据*/@RequestMapping("/test/map")public String testMap(Map map){//org.springframework.validation.support.BindingAwareModelMapSystem.out.println(map.getClass().getName());map.put("testRequestScope", "hello,map");return "success";}



测试通过ModelMap向请求域共享数据

/*3、使用ModelMap向请求域共享数据*/@RequestMapping("/test/modelMap")public String testModelMap(ModelMap modelMap){//org.springframework.validation.support.BindingAwareModelMapSystem.out.println(modelMap.getClass().getName());modelMap.addAttribute("testRequestScope", "hello,ModelMap");return "success";}



Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的
输入类型
System.out.println(modelMap.getClass().getName());
System.out.println(map.getClass().getName());
System.out.println(model.getClass().getName());


查看源码BindingAwareModelMap
按二次Shift

BindingAwareModelMap extends ExtendedModelMap

Ctrl+左健
ExtendedModelMap extends ModelMap

ModelMap extends LinkedHashMap

LinkedHashMapextends HashMap

总结:
5、Model和ModelMap和map的关系
* 其实在底层中,这些类型的形参最终都是通过BindingAwareModelMap创建
* public class BindingAwareModelMap extends ExtendedModelMap {}
* public class ExtendedModelMap extends ModelMap implements Model {}
* public class ModelMap extends LinkedHashMap {}
测试向会话域共享数据
测试向应用域共享数据

@RequestMapping("/test/session")public String testSession(HttpSession session){session.setAttribute("testSessionScope", "hello,session");return "success";}@RequestMapping("/test/application")public String testApplication(HttpSession session){ServletContext servletContext = session.getServletContext();servletContext.setAttribute("testApplicationScope", "hello,application");return "success";}





现在我们先把网址复制一下 然后 关闭浏览器
在打开浏览器 粘贴上刚才复制的网址
http://localhost:8080/springMVC/test/application

会现在这时session的数据没有了 只有application 因为application还没有关闭

现在我们从新部暑 然后在往seiion中共享一个数据





现在在重新重启服务器 重新部暑

