HTML-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="@{/testServletAPI}">获取request域中的数据</a><br>
    10. </body>
    11. </html>


    Controller:

    package com.way.controller;
    
    import com.way.pojo.User;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import javax.servlet.http.HttpServletRequest;
    
    @Controller
    public class MyController2 {
    
        @RequestMapping(value = "/testServletAPI")
        public String testServletAPI(HttpServletRequest request){
            request.setAttribute("testScope","hello");
            return "target";
        }
    }
    

    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="${testScope}"></p><!--testScope:是controller中request域中的key-->
    </body>
    </html>
    

    浏览器输出结果:
    image.png