持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第14天,点击查看活动详情
5、域对象共享数据
index.html
<a th:href="@{/test/mav}">测试通过ModelAndView向请求域共享数据</a><br>
<a th:href="@{/test/model}">测试通过Model向请求域共享数据</a><br>
<a th:href="@{/test/modelMap}">测试通过ModelMap向请求域共享数据</a><br>
<a th:href="@{/test/map}">测试通过map向请求域共享数据</a><br>
<a th:href="@{/test/session}">测试向会话域共享数据</a><br>
<a th:href="@{/test/application}">测试向应用域共享数据</a><br>
<hr>
<a th:href="@{/test/view/thymeleaf}">测试SpringMVC的视图ThymeleafView</a>
<a th:href="@{/test/view/forward}">测试SpringMVC的视图InternalResourceView</a>
<a th:href="@{/test/view/redirect}">测试SpringMVC的视图RedirectView</a>
</body>
success.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>成功</title>
</head>
<body>
<h1>success.html</h1>
<p th:text="${testRequestScope}"></p>
<p th:text="${session.testSessionScope}"></p>
<p th:text="${application.testApplication}"></p>
</body>
</html>
5.1、使用ServletAPI向request域对象共享数据
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope", "hello,servletAPI");
return "success";
}
5.2、使用ModelAndView向request域对象共享数据
@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;
}
5.3、使用Model向request域对象共享数据
@RequestMapping("/test/model")
public String testModel(Model model){
System.out.println(model.getClass().getName());//org.springframework.validation.support.BindingAwareModelMap
model.addAttribute("testRequestScope","hello,model");
return "success";
}
5.4、使用map向request域对象共享数据
@RequestMapping("/test/map")
public String testMap(Map<String,Object> map){
System.out.println(map.getClass().getName());//org.springframework.validation.support.BindingAwareModelMap
map.put("testRequestScope","hello,Map");
return "success";
}
5.5、使用ModelMap向request域对象共享数据
@RequestMapping("/test/modelMap")
public String testModelMap(ModelMap modelMap){
System.out.println(modelMap.getClass().getName());//org.springframework.validation.support.BindingAwareModelMap
modelMap.addAttribute("testRequestScope","hello,modelMap");
return "success";
}
5.6、Model、ModelMap、Map的关系
Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的
public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}
5.7、向session域共享数据
@RequestMapping("/test/session")
public String testSession(HttpSession session){
ServletContext servletContext = session.getServletContext();
session.setAttribute("testSessionScope","hello,session");
return "success";
}
5.8、向application域共享数据
@RequestMapping("/test/application")
public String testApplication(HttpSession session){
ServletContext servletContext = session.getServletContext();
servletContext.setAttribute("testApplication", "hello,application");
return "success";
}