• Springboot @RequestAttribute注解使用&getParameter和getAttribute区别


    参考资料

    1. 👍getParameter和getAttribute区别(超详细分析)
    2. 👍从原理层面掌握@RequestAttribute、@SessionAttribute的使用【享学Spring MVC】


    一. getParameter和getAttribute区别

    request.getParameter()
    在这里插入图片描述

    request.getAttribute()
    在这里插入图片描述
    ⏹二者区别

    • getParameter()获取的是客户端设置的数据。
      getAttribute()获取的是服务器设置的数据。
    • getParameter()永远返回字符串
      getAttribute()返回值是任意类型

    既然parameter和attribute都是传递参数,为什么不直接使用parameter呢?

    • 服务器端不能通过setParameter(key, value)来添加参数,因为没有这个函数
      所以如果需要在服务器端进行跳转,并需要想下个页面发送新的参数时,则没法实现。但是可以通过setAttribute(),将值放入到request对象,然后在其他页面使用getAttribute获取对应的值,这样就达到一次请求可以在多个页面共享一些对象信息
    • parameter返回值是字符串,意味着不能传递其他的对象,如Map,List,但是attribute则可以存放任意类型的Java对象

    二. @RequestAttribute注解的使用

    ⏹前台

    DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <script type="text/javascript" th:src="@{/js/public/jquery-3.6.0.min.js}">script>
        <script type="text/javascript" th:src="@{/js/common/common.js}">script>
        <title>test11页面title>
    head>
    <body>
        <button id="btn" type="button">发送get请求,传递参数button>
    body>
    <script>
        $("#btn").click((event) => {
    
            // 构造传递参数对象
            const urlSearchParams = new URLSearchParams();
            urlSearchParams.append("name", "贾飞天");
            urlSearchParams.append("age", "18");
    
            $.get(`/test11/getRequest?${urlSearchParams.toString()}`, function(data, status){
    
            });
        });
    script>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    ⏹后台Controller层

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    import java.util.Arrays;
    import java.util.List;
    
    @Controller
    @RequestMapping("/test11")
    public class Test11Controller {
    
        @Resource
        private HttpServletRequest request;
    
        @GetMapping("/init")
        public ModelAndView init() {
    
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("test11");
            return  modelAndView;
        }
    
        @GetMapping("/getRequest")
        public String getRequest(@RequestParam String name, @RequestParam Integer age) {
    
            System.out.println("年龄为:" + age);
    
            // 向请求书属性中放入相应值,用于转发时的数据传递
            request.setAttribute("orgName", name);
            request.setAttribute("orgInfo", Arrays.asList("张三", "李四"));
    
            return "forward:/test11/forward";
        }
    
        @GetMapping("/forward")
        public void forward(
                // 通过该注解接收request中的属性
                @RequestAttribute(value = "orgName" , required = false) String name,
                @RequestAttribute(value = "orgInfo") List<String> orgInfoList
        ) {
    
            System.out.println("姓名为:" + name);
            System.out.println("信息list为:" + orgInfoList);
    
            // 除了可以通过@RequestAttribute获取之外,还可以通过原生Servlet来获取
            List infoList = (List)request.getAttribute("orgInfo");
            System.out.println(infoList);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    在这里插入图片描述

  • 相关阅读:
    EDU挖掘
    GSCoolink GSV2702替EP92A3E HDMI 2.0交换机
    【Python】给出一个包含n个整数的数列,问整数a在数列中的第一次出现是第几个。
    NMEA0813协议简介
    线性回归算法之鸢尾花特征分类【机器学习】
    CSS盒子模型
    vue3+three.js实现疫情可视化
    【SLAM论文笔记】PL-EVIO笔记(中)
    收敛因子和黄金正弦指引机制的蝴蝶优化算法-附代码
    JUC 并发编程学习笔记(总)
  • 原文地址:https://blog.csdn.net/feyehong/article/details/126455009