- 使用ServletAPI向request域对象共享数据
| |
| @RequestMapping("/testServletAPI") |
| public String testServletAPI(HttpServletRequest request){ |
| request.setAttribute("testScope", "hello,servletAPI"); |
| return "success"; |
| } |
| |
| |
| <p th:text="${testScope}"></p> |
| |
| |
| http://localhost:8080/上下文路径/testServletAPI |
- 使用ModelAndView向request域对象共享数据
| # 后端:创建ModelAndView对象,并将对象返回给前端控制器,前端控制器解析对象 |
| @RequestMapping("/testModelAndView") |
| public ModelAndView testModelAndView(){ |
| |
| |
| |
| |
| |
| ModelAndView mav = new ModelAndView(); |
| |
| mav.addObject("testScope", "hello,ModelAndView"); |
| |
| mav.setViewName("success"); |
| return mav; |
| } |
| |
| # thymeleaf编写的success页面中获取数据 |
| <p th:text="${testScope}"></p> |
| |
| # 浏览器访问如下 |
| http: |
| @RequestMapping("/testModel") |
| public String testModel(Model model){ |
| model.addAttribute("testScope", "hello,Model"); |
| return "success"; |
| } |
| |
| |
| <p th:text="${testScope}"></p> |
| |
| |
| http://localhost:8080/上下文路径/testModel |
| @RequestMapping("/testMap") |
| public String testMap(Map<String, Object> map){ |
| map.put("testScope", "hello,Map"); |
| return "success"; |
| } |
| |
| # thymeleaf编写的success页面中获取数据 |
| <p th:text="${testScope}"></p> |
| |
| # 浏览器访问如下 |
| http: |
- 使用ModelMap向request域对象共享数据
| @RequestMapping("/testModelMap") |
| public String testModelMap(ModelMap modelMap){ |
| modelMap.addAttribute("testScope", "hello,ModelMap"); |
| return "success"; |
| } |
| |
| |
| <p th:text="${testScope}"></p> |
| |
| |
| http://localhost:8080/上下文路径/testModelMap |
| @RequestMapping("/testSession") |
| public String testSession(HttpSession session){ |
| session.setAttribute("testSessionScope", "hello,session"); |
| return "success"; |
| } |
| |
| |
| <p th:text="${session.testSessionScope}"></p> |
| |
| |
| http://localhost:8080/上下文路径/testSession |
| @RequestMapping("/testApplication") |
| public String testApplication(HttpSession session){ |
| ServletContext application = session.getServletContext(); |
| application.setAttribute("testApplicationScope", "hello,application"); |
| return "success"; |
| } |
| |
| |
| <p th:text="${application.testApplicationScope}"></p> |
| |
| |
| http://localhost:8080/上下文路径/testApplication |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?