• 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
  • 相关阅读:
    服务器cpu和普通cpu有哪些不同
    QT中软件cpu占用率很高,甚至达到了50% 62%左右
    网络业务创新驱动下的DPU P4技术,中科驭数在网络开源技术生态大会上分享最新进展
    【qml】 QML属性绑定的三种方法
    《InnoDB引擎七》InnoDB关键特性-插入缓存
    使用Inis搭配内网穿透实现Ubuntu上快速搭建博客网站远程访问
    华为OD机试真题-堆内存申请-2023年OD统一考试(C卷D卷)
    windows下app爬虫环境搭建:python + fiddler + Appium + 夜神模拟器
    计算机毕业论文选题推荐|软件工程|系列八
    数据结构--栈
  • 原文地址:https://blog.csdn.net/weixin_46245201/article/details/126100919