域对象共享数据
使用ServletAPI向request域对象共享数据
1 2 3 4 5
| @RequestMapping("/testServletAPI") public String testServletAPI(HttpServletRequest request){ request.setAttribute("testScope", "hello,servletAPI"); return "success"; }
|
使用ModelAndView向request域对象共享数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @RequestMapping("/testModelAndView") public ModelAndView testModelAndView(){
ModelAndView mav = new ModelAndView(); mav.addObject("testScope", "hello,ModelAndView"); mav.setViewName("success"); return mav; }
|
使用Model向request域对象共享数据
1 2 3 4 5
| @RequestMapping("/testModel") public String testModel(Model model){ model.addAttribute("testScope", "hello,Model"); return "success"; }
|
使用map向request域对象共享数据
1 2 3 4 5
| @RequestMapping("/testMap") public String testMap(Map<String, Object> map){ map.put("testScope", "hello,Map"); return "success"; }
|
使用ModelMap向request域对象共享数据
1 2 3 4 5
| @RequestMapping("/testModelMap") public String testModelMap(ModelMap modelMap){ modelMap.addAttribute("testScope", "hello,ModelMap"); return "success"; }
|
Model、ModelMap、Map的关系
Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型*的
1 2 3 4
| public interface Model{} public class ModelMap extends LinkedHashMap<String, Object> {} public class ExtendedModelMap extends ModelMap implements Model {} public class BindingAwareModelMap extends ExtendedModelMap {}
|
向session域共享数据
1 2 3 4 5 6
| @RequestMapping("/testApplication") public String testApplication(HttpSession session){ ServletContext application = session.getServletContext(); application.setAttribute("testApplicationScope", "hello,application"); return "success"; }
|
向application域共享数据
1 2 3 4 5 6
| @RequestMapping("/testApplication") public String testApplication(HttpSession session){ ServletContext application = session.getServletContext(); application.setAttribute("testApplicationScope", "hello,application"); return "success"; }
|
完整代码:
1 2 3 4 5 6 7 8 9
| @Controller public class TestController {
@RequestMapping("/") public String index(){ return "index"; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| @Controller public class ScopeController {
@RequestMapping("/testRequestByservletAPI") public String testRequestByServletAPI(HttpServletRequest request){ request.setAttribute("testRequestScope","helloServletAPI"); return "success"; } @RequestMapping("/testModelAndView") public ModelAndView testModeAndView(){ ModelAndView mav = new ModelAndView(); mav.addObject("testRequestScope","hello.ModelAndView"); mav.setViewName("success"); return mav; }
@RequestMapping("/testModel") public String testModel(Model model){ model.addAttribute("testRequestScope","hello,model"); return "success"; }
@RequestMapping("/testMap") public String testMap(Map<String,Object> map){ map.put("testRequestScope","hello,map"); return "success";
}
@RequestMapping("/testModelMap") public String testModeMap(ModelMap modelMap){ modelMap.addAttribute("testRequestScope","hello,ModelMap"); return "success";
} @RequestMapping("/testSession") public String testSession(HttpSession session){ session.setAttribute("testSessionScope","Hello,Session");
return "success"; }
@RequestMapping("/testApplication") public String testApplication(HttpSession session){ ServletContext application = session.getServletContext(); application.setAttribute("testSessionScope","Hello,application"); return "success"; } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> success<br> <p th:text="${testRequestScope}"></p> <p th:text="${session.testSessionScope}"></p> </body> </html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>首页</h1> <a th:href="@{/testRequestByservletAPI}">通过servletAPI向request域对象共享数据</a><br> <a th:href="@{/testModelAndView}">通过ModelAndView向request域对象共享数据</a><br> <a th:href="@{/testModel}">通过Model向request域对象共享数据</a><br> <a th:href="@{/testMap}">通过Map向request域对象共享数据</a><br> <a th:href="@{/testModelMap}">通过ModelMap向request域对象共享数据</a><br> <a th:href="@{/testSession}">通过ServletAPI向Session域对象共享数据</a><br> <a th:href="@{/testApplication}">通过Application向Session域对象共享数据</a><br> </body> </html>
|
参考:https://frxcat.fun/