spring-mvc 系列:域对象共享数据

发布时间 2023-08-06 00:10:11作者: IT技术派

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

html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
   <h1>index</h1>
   <a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
</body>
</html>

java

package com.mcode.api.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;

/**
 * ClassName: ScopeController
 * Package: com.mcode.api.controller
 * Description:
 *
 * @Author: robin
 * @Create: 2023/8/5 - 11:06 AM
 * @Version: v1.0
 */
@Controller
public class ScopeController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }

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

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

html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
   <h1>index</h1>
   <a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
   <a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
</body>
</html>

java

package com.mcode.api.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;

/**
 * ClassName: ScopeController
 * Package: com.mcode.api.controller
 * Description:
 *
 * @Author: robin
 * @Create: 2023/8/5 - 11:06 AM
 * @Version: v1.0
 */
@Controller
public class ScopeController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }

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

    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("testScope","hello,ModelAndView");
        modelAndView.setViewName("success");
        return modelAndView;
    }
}

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

html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
   <h1>index</h1>
   <a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
   <a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
   <a th:href="@{/testModel}">测试Model--->/testModel</a>
</body>
</html>

java

package com.mcode.api.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;

/**
 * ClassName: ScopeController
 * Package: com.mcode.api.controller
 * Description:
 *
 * @Author: robin
 * @Create: 2023/8/5 - 11:06 AM
 * @Version: v1.0
 */
@Controller
public class ScopeController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }

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

    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("testScope","hello,ModelAndView");
        modelAndView.setViewName("success");
        return modelAndView;
    }

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

四、使用Map向request域对象共享数据

html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
   <h1>index</h1>
   <a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
   <a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
   <a th:href="@{/testModel}">测试Model--->/testModel</a>
   <a th:href="@{/testMap}">测试Map--->/testMap</a>
</body>
</html>

java

package com.mcode.api.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;

/**
 * ClassName: ScopeController
 * Package: com.mcode.api.controller
 * Description:
 *
 * @Author: robin
 * @Create: 2023/8/5 - 11:06 AM
 * @Version: v1.0
 */
@Controller
public class ScopeController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }

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

    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("testScope","hello,ModelAndView");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @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";
    }
}

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

html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
   <h1>index</h1>
   <a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
   <a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
   <a th:href="@{/testModel}">测试Model--->/testModel</a>
   <a th:href="@{/testMap}">测试Map--->/testMap</a>
   <a th:href="@{/testModelMap}">测试ModelMap--->/testModelMap</a>
</body>
</html>

java

package com.mcode.api.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;

/**
 * ClassName: ScopeController
 * Package: com.mcode.api.controller
 * Description:
 *
 * @Author: robin
 * @Create: 2023/8/5 - 11:06 AM
 * @Version: v1.0
 */
@Controller
public class ScopeController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }

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

    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("testScope","hello,ModelAndView");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @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.put("testScope", "hello,ModelMap");
        return "success";
    }
}

六、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 {}

七、向session域共享数据

html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
   <h1>index</h1>
   <a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
   <a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
   <a th:href="@{/testModel}">测试Model--->/testModel</a>
   <a th:href="@{/testMap}">测试Map--->/testMap</a>
   <a th:href="@{/testModelMap}">测试ModelMap--->/testModelMap</a>
   <a th:href="@{/testSession}">测试Session--->/testSession</a>
</body>
</html>

java

package com.mcode.api.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;

/**
 * ClassName: ScopeController
 * Package: com.mcode.api.controller
 * Description:
 *
 * @Author: robin
 * @Create: 2023/8/5 - 11:06 AM
 * @Version: v1.0
 */
@Controller
public class ScopeController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }

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

    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("testScope","hello,ModelAndView");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @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.put("testScope", "hello,ModelMap");
        return "success";
    }

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

八、向application域共享数据

html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
   <h1>index</h1>
   <a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
   <a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
   <a th:href="@{/testModel}">测试Model--->/testModel</a>
   <a th:href="@{/testMap}">测试Map--->/testMap</a>
   <a th:href="@{/testModelMap}">测试ModelMap--->/testModelMap</a>
   <a th:href="@{/testSession}">测试Session--->/testSession</a>
   <a th:href="@{/testApplication}">测试Application--->/testApplication</a>
</body>
</html>

java

package com.mcode.api.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;

/**
 * ClassName: ScopeController
 * Package: com.mcode.api.controller
 * Description:
 *
 * @Author: robin
 * @Create: 2023/8/5 - 11:06 AM
 * @Version: v1.0
 */
@Controller
public class ScopeController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }

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

    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("testScope","hello,ModelAndView");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @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.put("testScope", "hello,ModelMap");
        return "success";
    }

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

    @RequestMapping("/testApplication")
    public String testApplication(HttpSession session){
        ServletContext servletContext = session.getServletContext();
        servletContext.setAttribute("testApplicationScope","hello,application");
        return "success";
    }
}