6. SpringMVC-域对象共享数据

零、四大域对象

1. PageContext域

  1. 生命周期:

     当对JSP的请求开始,当相应结束时销毁。
     jsp页面被执行,声明周期开始;
     jsp页面执行完毕,声明周期结束;
    
  2. 作用范围:整个JSP页面,是四大作用域中最小的一个。

2. Request域

  1. 生命周期:

     在Service方法调用前由服务器创建,传入service方法。整个请求结束,request生命结束。
     用户发送一个请求,开始,服务器返回响应,请求结束,生命周期结束;
    
  2. 作用范围:整个请求链(请求转发也存在)

3. HttpSession 域

  1. 生命周期:

     在第一次调用request.getSession()方法时,服务器会检查是否已经有对应的session,如果没有就在内存中创建一个session并返回。
     (1)当一段时间内session没有被使用(默认为30分钟),则服务器会销毁该session。
     (2)如果服务器非正常关闭,没有到期的session也会跟着销毁。
     (3)如果调用session提供的invalidate(),可以立即销毁session。
     用户打开浏览器访问,创建session(开始),session超时或者被声明失效,该对象生命周期结束;
    
  2. 作用范围:一次会话。

4. ServletContext

  1. 生命周期:

     当WEB应用被加载进容器创建代表整个WEB应用的ServletContext对象;
     当服务器关闭或WEB应用被移除时,ServletContext对象跟着被销毁。
    
  2. 作用范围:整个WEB应用。

一、创建项目

  1. 新建maven工程
  2. 引入pom.xml依赖
  3. 创建webapp模块
  4. 配置web.xml文件
  5. 配置springMVC.xml文件
    详细过程参考-链接: https://blog.csdn.net/hznb_369/article/details/122767827?utm_source=app&app_version=5.0.1&code=app_1562916241&uLinkId=usr1mkqgl919blen.

二、向request域对象共享数据

1. 使用ServletAPI向request域对象共享数据

  1. controller方法
	//使用servletAPI向servletAPI域对象共享数据
    @RequestMapping("/testRequestByServletAPI")
    public String testRequestByServletAPI(HttpServletRequest request){
        request.setAttribute("testRequestScop","hello , servletAPI");
        //请求转发
        return "success";
    }
  1. success.html页面接收数据
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>success</h1>
<br>
<!--获取request域中的数据-->
<p th:text="${testRequestScop}"></p>
</body>
</html>
  1. 启动服务器访问index.html页面中的测试方法
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testRequestByServletAPI}">通过servletAPI向request域对象共享数据</a><br>
</body>
</html>

在这里插入图片描述

2. 使用ModelAndView向request域对象共享数据

  1. 创建方法,返回值必须是ModelAndView
	@RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        /**
         * ModelAndView有Model和View的功能
         * Model主要用于向请求域共享数据
         * View主要用于设置视图,实现页面跳转
         */
        ModelAndView mav = new ModelAndView();
        //向请求域共享数据
        mav.addObject("testScope", "hello,ModelAndView");
        //设置视图,实现页面跳转
        mav.setViewName("success");
        return mav;
    }

3. 使用Model向request域对象共享数据

  1. testModel方法
	@RequestMapping("/testModel")
    public String testModel(Model model){
        model.addAttribute("testRequestScop", "hello,Model");
        System.out.println(model.getClass().getName());
        return "success";
    }
  1. 访问:http://localhost:8080/SpringMvc03/testModel
    在这里插入图片描述

4. 使用map向request域对象共享数据

  1. testMap
	@RequestMapping("/testMap")
    public String testMap(Map<String,Object> map){
        map.put("testRequestScop","hello ,map");
        System.out.println(map.getClass().getName());
        return "success";
    }
  1. 访问http://localhost:8080/SpringMvc03/testMap
    在这里插入图片描述

5. 使用ModelMap向request域对象共享数据

  1. testModelMap
	@RequestMapping("/testModelMap")
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("testRequestScop","hello ,modelMap");
        System.out.println(modelMap.getClass().getName());
        return "success";
    }
  1. 访问http://localhost:8080/SpringMvc03/testModelMap
    在这里插入图片描述

6. Model、ModelMap、Map的关系

  1. 依次访问:http://localhost:8080/SpringMvc03/testModel、http://localhost:8080/SpringMvc03/testMap、http://localhost:8080/SpringMvc03/testModelMap

  2. 观察打印结果:
    在这里插入图片描述

  3. 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 {}

三、向session域共享数据

  1. testSession
@RequestMapping("/testSession")
public String testSession(HttpSession session){
    session.setAttribute("testSessionScope", "hello,session");
    return "success";
}
  1. 接收数据
<!--获取session域中的数据 -->
<p th:text="${session.testSessionScop}"></p>
  1. 访问链接:http://localhost:8080/SpringMvc03/testSession
    在这里插入图片描述

四、向application域共享数据

  1. testApplication
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
	ServletContext application = session.getServletContext();
    application.setAttribute("testApplicationScope", "hello,application");
    return "success";
}
  1. 接收数据
<!--获取servletContext域中的数据&ndash;&gt;-->
<p th:text="${application.testApplicationScope}"></p>
  1. 访问链接:http://localhost:8080/SpringMvc03/testApplication
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CAFEBABE 34

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值