springMVC域对象共享数据

文章目录

五、域对象共享数据

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

html 复制代码
<a th:href="@{testServletAPI}">测试通过ServletAPI向请求域共享数据</a>
java 复制代码
@RequestMapping("/testServletAPI")  public String testServletAPI(HttpServletRequest request){   request.setAttribute("testScope","hello,servletAPI");   return "success"; }
html 复制代码
<p th:text="${testScope}"></p>

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

html 复制代码
<a th:href="@{/testmav}">测试通过ModelAndView向请求域共享数据</a>
java 复制代码
@RequestMapping("/testmav")
public ModelAndView testmav(){
    /**
     * ModelAndView包含Model和View的功能
     * Model:向请求域中共享数据
     * View:设置逻辑视图实现页面跳转
     */
    ModelAndView modelAndView = new ModelAndView();
    //向请求域中共享数据
    modelAndView.addObject("testReqeustScope","hello,ModelAndView");
    //设置逻辑视图
    modelAndView.setViewName("success");
    //使用View设置逻辑视图,但是控制器方法一定要将ModelAndView的对象作为方法的返回值
    return modelAndView;
}
html 复制代码
<p th:text="${testReqeustScope}"></p>

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

html 复制代码
<a th:href="@{/testmodel}">测试通过Model向请求域共享数据</a><br>
java 复制代码
@RequestMapping("/testmodel")
public String testmodel(Model model){
    //org.springframework.validation.support.BindingAwareModelMap
    System.out.println(model.getClass().getName());
    model.addAttribute("testReqeustScope","hello,Model");
    return "success";
}

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

java 复制代码
@RequestMapping("/testMap")
public String testMap(Map<String,Object> map){
    //org.springframework.validation.support.BindingAwareModelMap
    System.out.println(map.getClass().getName());
    map.put("testReqeustScope","hello,map");
    return "success";
}

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

java 复制代码
@RequestMapping("/testmodelmap")
public String testModelMap(ModelMap modelMap){
    //org.springframework.validation.support.BindingAwareModelMap
    System.out.println(modelMap.getClass().getName());
    modelMap.addAttribute("testReqeustScope","hello,ModelMap");
    return "success";
}

5.6 Model、ModelMap、Map的关系

Model、ModelMap、Map类型的参数本质上其实都是BindingAwareModelMap类型的

org.springframework.validation.support.BindingAwareModelMap

所以在底层中,这些类型的形参最终都是通过BindingAwareModelMap创建的

5.7、向session域共享数据

html 复制代码
<a th:href="@{/testsession}">测试通过session向回话域共享数据</a><br>
java 复制代码
@RequestMapping("/testsession")
public String testSession(HttpSession session){
    session.setAttribute("testSessionScope","hello,session");
    return "success";
}
html 复制代码
<p th:text="${session.testSessionScope}"></p>

5.8、向application域共享数据

java 复制代码
@RequestMapping("/testapplication")
public String testApplication(HttpSession session){
    ServletContext servletContext = session.getServletContext();
    servletContext.setAttribute("testApplicationScope","hello,Application");
    return "success";
}
html 复制代码
*<!--**通过**${application.**属性名**}**获取的是**application**域对象数据**--> *  <p th:text="${application.testApplicationScope}"></p>

}
html 复制代码
*<!--**通过**${application.**属性名**}**获取的是**application**域对象数据**--> *  <p th:text="${application.testApplicationScope}"></p>
相关推荐
Highcharts.js4 小时前
教程:基于 React + Highcharts 构建一个单页应用程序、按需数据拉取与图表渲染
开发语言·前端·数据结构·react.js·前端框架·highcharts·页面应用
头发还在的女程序员5 小时前
医院陪诊管理系统怎么选择?——2026 年选型避坑与架构参考
java·开发语言·陪诊系统·陪诊app·医院陪诊陪护
爱写代码的小朋友7 小时前
从零开始学 Win32 API:C++ 窗口编程实战(VS Code + MinGW-w64 命令行详解)
开发语言·c++
果汁华7 小时前
Function Calling 与 Python 实战完整指南
开发语言·网络·python
小小晓.7 小时前
C++:语句和作用域
开发语言·c++
wanderist.8 小时前
Lambda表达式在算法竞赛中的应用
java·开发语言·算法
海天鹰9 小时前
PHP上传文件
android·开发语言·php
Yeauty11 小时前
渲染成图再 CLI 拼接,还是进程内直推?Rust 帧到视频的两条路
开发语言·rust·音视频
geovindu11 小时前
CSharp: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·算法·c#·.net·搜索算法
库克克12 小时前
【C++】set 与multiset
开发语言·c++