04-SpringMVC 域对象共享数据(后端数据传送给前端)

1.前端编码

index.xml

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>hello world!</h1>
    <a th:href="@{/testServletAPI}">1.使用ServletAPI向request域对象共享数据</a><br>
    <a th:href="@{/testModelAndView}">2.使用ModelAndView向request域对象共享数据</a><br>
    <a th:href="@{/testModel}">3.使用Model向request域对象共享数据</a><br>
    <a th:href="@{/testMap}">4.使用Map向request域对象共享数据</a><br>
    <a th:href="@{/testModelMap}">5.使用ModelMap向request域对象共享数据</a><br>
    <a th:href="@{/testSession}">6.使用ServletAPI向Session域共享数据</a><br>
    <a th:href="@{/testApplication}">7.使用ServletAPI向application域共享数据</a><br>

</body>
</html>

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>
<p th:text="${testScope}"></p>
<p th:text="${session.testScope}"></p>
<p th:text="${application.testScope}"></p>
</body>
</html>

在这里插入图片描述

2.域对象共享数据方法

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

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

在这里插入图片描述

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

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

在这里插入图片描述

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

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

在这里插入图片描述

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

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

在这里插入图片描述

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

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

在这里插入图片描述

6.使用ServletAPI向Session域共享数据

    @RequestMapping("/testSession")
    public String testHttpSession(HttpSession httpSession){
        httpSession.setAttribute("testScope", "hello,Session");
        return "success";
    }

在这里插入图片描述

7.使用ServletAPI向application域共享数据

    @RequestMapping("/testApplication")
    public String testApplication(HttpSession httpSession){
        ServletContext application = httpSession.getServletContext();
        application.setAttribute("testScope", "hello,Application");
        return "success";
    }

在这里插入图片描述

3.后端编码

package com.limi.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {

    @RequestMapping("/index")
    public String test(){
        return "index";
    }

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

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

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

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

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

    @RequestMapping("/testSession")
    public String testHttpSession(HttpSession httpSession){
        httpSession.setAttribute("testScope", "hello,Session");
        return "success";
    }

    @RequestMapping("/testApplication")
    public String testApplication(HttpSession httpSession){
        ServletContext application = httpSession.getServletContext();
        application.setAttribute("testScope", "hello,Application");
        return "success";
    }

}

4.小知识

1.Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的, 就是这三个最后都会被BindingAwareModelMap所继承或实现, 从而实例化成BindingAwareModelMap 类型.

public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}

2.session中的数据默认会保存30分钟, 若30分钟该数据仍然未被访问, 则会清除.
3.session一般可以用来保存登录信息.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值