游侠的博客 游侠的博客
首页
  • 论文笔记
  • 一些小知识点

    • pytorch、numpy、pandas函数简易解释
  • 《深度学习500问》
开发
技术
更多
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Ranger

一名在校研究生
首页
  • 论文笔记
  • 一些小知识点

    • pytorch、numpy、pandas函数简易解释
  • 《深度学习500问》
开发
技术
更多
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Vue

  • SpringBoot2

  • JavaWeb

  • SSM

    • Spring

    • Mybatis

    • SpringMVC

      • 一、SpringMVC详解
      • 二、SpringMVC入门
      • 三、RequestMapping注解
      • 四、SpringMVC获取请求参数
      • 五、域对象共享数据
        • 5.1、使用ServletAPI向request域对象共享数据
        • 5.2、使用ModelAndView向request域对象共享数据
        • 5.3、使用Model向request域对象共享数据
        • 5.4、使用map向request域对象共享数据
        • 5.6、Model、ModelMap、Map的关系
        • 5.7、向session域共享数据
        • 5.8、向application域共享数据
      • 六、SpringMVC的视图
      • 七、RESTful风格
      • 八、RESTful案例
      • 九、SpringMVC处理Ajax请求
      • 十、文件上传和下载
      • 十一、拦截器
      • 十二、异常处理器
      • 十三、注解配置SpringMVC
      • 十四、SpringMVC执行流程
      • 其他知识点
    • SSM整合
  • SpringBoot3

  • 技术
  • SSM
  • SpringMVC
yangzhixuan
2023-04-23
目录

五、域对象共享数据

Thymeleaf获取数据代码如下

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>success.html</h1>
<p th:text="${testScope}"></p>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11

# 5.1、使用ServletAPI向request域对象共享数据

@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
    request.setAttribute("testScope", "hello,servletAPI");
    return "success";
}
1
2
3
4
5

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

@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
    /**
     * ModelAndView有Model和View的功能
     * Model主要用于向请求域共享数据
     * View主要用于设置视图,实现页面跳转
     */
    ModelAndView mav = new ModelAndView();
    //向请求域共享数据
    mav.addObject("testScope", "hello,ModelAndView");
    //设置视图,实现页面跳转
    mav.setViewName("success");
    return mav;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

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

@RequestMapping("/testModel")
public String testModel(Model model){
    model.addAttribute("testScope", "hello,Model");
    return "success";
}
1
2
3
4
5

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

@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
    map.put("testScope", "hello,Map");
    return "success";
}
1
2
3
4
5

5.5、使用ModelMap向request域对象共享数据

@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
    modelMap.addAttribute("testScope", "hello,ModelMap");
    return "success";
}
1
2
3
4
5

# 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 {}
1
2
3
4

# 5.7、向session域共享数据

@RequestMapping("/testSession")
public String testSession(HttpSession session){
    session.setAttribute("testSessionScope", "hello,session");
    return "success";
}
1
2
3
4
5

当服务器关闭时,当前的session会被钝化,session会被保存在服务器的磁盘文件上,下次启动时这些session会被活化

# 5.8、向application域共享数据

@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
    ServletContext application = session.getServletContext();
    application.setAttribute("testApplicationScope", "hello,application");
    return "success";
}
1
2
3
4
5
6
编辑 (opens new window)
上次更新: 2024/05/30, 07:49:34
四、SpringMVC获取请求参数
六、SpringMVC的视图

← 四、SpringMVC获取请求参数 六、SpringMVC的视图→

最近更新
01
tensor比较大小函数
05-30
02
Large Language Models can Deliver Accurate and Interpretable Time Series Anomaly Detection
05-27
03
半监督学习经典方法 Π-model、Mean Teacher
04-10
更多文章>
Theme by Vdoing | Copyright © 2023-2024 Ranger | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式