• springmvc域对象共享数据:request、sssion、application


    一、通过原生servletAPI向request域中共享数据

    DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    
    <h1>successh1><br/>
    servletapi:<p th:text="${name}">p><br/>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    @RequestMapping("testRequestByServletAPI")
        public String testRequestByServletAPI(HttpServletRequest request) {
            request.setAttribute("name", "job");
            return "success";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    二、使用ModelAndView向request域对象共享数据

    不管用的是什么方法,最后都会封装成ModelAndView对象。

    DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    
    <h1>successh1><br/>
    servletapi:<p th:text="${name}">p><br/>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    @RequestMapping("testRequestByModelAndView")
        public ModelAndView testRequestByModelAndView() {
            ModelAndView mav = new ModelAndView();
            //向域对象中共享数据
            mav.addObject("name", "modelAmdView");
            //设置视图名称
            mav.setViewName("success");
            return mav;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    三、使用Model向request域对象共享数据

    DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    
    <h1>successh1><br/>
    servletapi:<p th:text="${name}">p><br/>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    @RequestMapping("testRequestByModel")
        public String testRequestByModel(Model model) {
            model.addAttribute("name", "model");
            return "success";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    四、使用map向request域对象共享数据

    DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    
    <h1>successh1><br/>
    servletapi:<p th:text="${name}">p><br/>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    @RequestMapping("testRequestByMap")
        public String testRequestByMap(Map<String,Object> map) {
            map.put("name","map");
            return "success";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    五、使用modelMap向request域对象共享数据

    DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    
    <h1>successh1><br/>
    servletapi:<p th:text="${name}">p><br/>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    @RequestMapping("testRequestByModelMap")
        public String testRequestByModelMap(ModelMap modelMap) {
            modelMap.addAttribute("name", "modelMap");
            return "success";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    六、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 {}
    
    • 1
    • 2
    • 3
    • 4

    七、向session域中共享数据

    DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    
    <h1>successh1><br/>
    request:<p th:text="${name}">p><br/>
    session:<p th:text="${session.name}">p><br/>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    @RequestMapping("tesSessionByServletApi")
        public String tesSessionByServletApi(HttpSession session){
            session.setAttribute("name","session");
            return "success";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    八、向application域共享数据

    DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    
    <h1>successh1><br/>
    request:<p th:text="${name}">p><br/>
    session:<p th:text="${session.name}">p><br/>
    application:<p th:text="${application.name}">p><br/>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    @RequestMapping("testApplicationByServletApi")
        public String testApplicationByServletApi(HttpSession session){
            ServletContext application = session.getServletContext();
            application.setAttribute("name","application");
            return "success";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    【postgresql 基础入门】基础架构和命名空间层次,查看数据库对象再也不迷路
    接口自动化测试用例如何设计
    java计算机毕业设计共享汽车管理系统MyBatis+系统+LW文档+源码+调试部署
    安卓循环遍历计时器
    BEiT-3论文阅读笔记
    计算机毕业设计(附源码)python中学教务管理系统
    js将一组数据从大到小排序方法
    【JavaWeb】Servlet系列——使用纯Servlet做一个单表的CRUD操作(实际操作实现篇)
    如何让论文成为一篇可发表的期刊文章?
    前端包管理器:深入理解npm和yarn
  • 原文地址:https://blog.csdn.net/weixin_46245201/article/details/126100919