SpringMVC-域对象共享数据


目录
  • 环境
  • request
    • ServletAPI
    • ModelAndView
    • Model
    • map
    • ModelMap
    • Model、ModelMap、Map的关系
  • session
  • application

环境

IDE:idea 2020.3
构建工具:maven-3.8.1
服务器:tomcat9
Spring版本:5.3.1


    
    
        org.springframework
        spring-webmvc
        5.3.1
    

    
    
        ch.qos.logback
        logback-classic
        1.2.3
    

    
    
        javax.servlet
        javax.servlet-api
        3.1.0
        provided
    

    
    
        org.thymeleaf
        thymeleaf-spring5
        3.0.12.RELEASE
    

<?xml version="1.0" encoding="UTF-8"?>



    
    

    
    
        
        
        
            
                
                     
                    
                    
                    
                    
                    
                    
                    
                 
            
        
    


 
    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceResponseEncoding
            true
        
    

    
        CharacterEncodingFilter
        /*
    

    
        DispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springMVC.xml
        
        1
    

    
        DispatcherServlet
        /
    

request

ServletAPI

package com.fly.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 26414
 */
@Controller
public class IndexController {

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

  @RequestMapping("/testServlet")
  public String testServlet(HttpServletRequest request) {
    request.setAttribute("testScope","servlet");
    return "hello";
  }

}




  
  首页



通过ServletAPI向request存放数据





  
  hello



hello

ModelAndView

  /**
   *IndexController
   */
  @RequestMapping("/testModelAndView")
  public ModelAndView testModelAndView(ModelAndView modelAndView) {
    //向请求域共享数据
    modelAndView.addObject("testScope","ModelAndView");
    //设置视图
    modelAndView.setViewName("hello");
    return modelAndView;
  }

通过ModelAndView向request存放数据

Model

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

通过Model向request存放数据

map

 /**
   *IndexController
   */
  @RequestMapping("/testMap")
  public String testMap(Map map) {
    map.put("testScope","map");
    return "hello";
  }

通过Map向request存放数据

ModelMap

 /**
   *IndexController
   */
  @RequestMapping("/testModelMap")
  public String testModelMap(ModelMap map) {
    map.put("testScope","modelMap");
    return "hello";
  }

通过ModelMap向request存放数据

Model、ModelMap、Map的关系

在控制台打印这三个对象的信息


可以发现这三个对象都是BindingAwareModelMap类型的


在几个方法上打断点,用debug模式启动服务器,访问/testServlet,可以在方法栈中看到前端控制器的方法


F9跳过这个断点,进入下一个断点,可以发现数据被封装到一个ModelAndView上

访问/testModelAndView

访问/testModel

最后得出结论,使用Model、ModelMap、Map最后数据都会封装到ModelAndView上

session

  /**
   *IndexController
   */
  @RequestMapping("/testSession")
  public String testSession(HttpSession session) {
    session.setAttribute("testSessionInfo","session");
    return "hello";
  }

通过ServletAPI向session存放数据

application

 /**
   *IndexController
   */
  @RequestMapping("/testApplication")
  public String testApplication(HttpSession session) {
    ServletContext application = session.getServletContext();
    application.setAttribute("testApplicationInfo","application");
    return "hello";
  }

通过ServletAPI向application存放数据