Springboot forward请求转发 传递数据

参考资料

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

目录

  • 一. getParameter和getAttribute区别
  • 二. @RequestAttribute注解的使用


一. getParameter和getAttribute区别

request.getParameter()
Springboot forward请求转发 传递数据_第1张图片

request.getAttribute()
Springboot forward请求转发 传递数据_第2张图片
⏹二者区别

  • 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>

⏹后台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);
    }
}

Springboot forward请求转发 传递数据_第3张图片

你可能感兴趣的:(SpringBoot,spring,boot,spring)