index:

    1. <!DOCTYPE html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. </head>
    7. <body>
    8. <h1>首页</h1>
    9. <a th:href="@{/testModelAndView}">获取request域中的数据</a><br>
    10. </body>
    11. </html>

    controller:

    1. package com.way.controller;
    2. import com.way.pojo.User;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.servlet.ModelAndView;
    6. import javax.servlet.http.HttpServletRequest;
    7. @Controller
    8. public class MyController2 {
    9. @RequestMapping(value = "/testModelAndView")
    10. public ModelAndView testModelAndView(){
    11. ModelAndView modelAndView = new ModelAndView();
    12. //保存到request中
    13. modelAndView.addObject("key1","helloRequest1");
    14. modelAndView.addObject("key2","helloRequest2");
    15. //转发到target页面
    16. modelAndView.setViewName("target");
    17. return modelAndView;
    18. }
    19. }

    target页面:

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <h1>target页面</h1>
        <p th:text="${key1}"></p><!--testScope:是controller中request域中的key1的value-->
        <p th:text="${key2}"></p><!--testScope:是controller中request域中的key2的value-->
    </body>
    </html>
    

    浏览器输出:
    image.png