首页 > 其他分享 >第03章 SpringBoot获取请求参数

第03章 SpringBoot获取请求参数

时间:2024-10-14 11:18:59浏览次数:3  
标签:03 String age boot name test 请求 SpringBoot

我们首先创建 “SpringBootRequestDemo” 工程。

然后我们修改编码格式以及Maven仓库地址,我们省略这个过程了。 接着我们再添加spring-boot-starter-parent,spring-boot-starter-web,spring-boot-starter-thymeleaf依赖库

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.demo</groupId>
    <artifactId>SpringBootRequestDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.13</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.7.18</version>
            </plugin>
        </plugins>
    </build>

</project>

我们继续创建“Application”的入口类

package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
    }
}

这个文件基本上也是固定不变的。

接下来,我们创建 “resources\static\index.html” 入口文件

<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>index</title>
</head>
<body>

<p><a href="/test">test</a></p>

</body>
</html>

我们做了一个 “/test” 的请求,然后我们创建 TestController 来处理这个请求

package com.demo;

import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {

    @RequestMapping("/test")
    public String hello(Map<String, Object> map) {

        map.put("message", "hello test!");
        return "test";
    }
    
}

我们向“test.html” 输出一个 “message” 的数据,继续创建 “resources\templates\test.html” 文件。

<!doctype html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>test</title>
</head>
<body>

<h1 th:text="${message}"></h1>

</body>
</html>

我们使用上一章节的 “Thymeleaf” 模版输出列表数据。

我们运行整个项目。

Spring Boot获取前端页面参数的几种方式总结

第一,指定前端url请求参数名称与方法参数名一致, 适用get方式,而不适用post方式。

例如请求:http://localhost:8080/test01?name=zhang&age=30
    @RequestMapping("/test01")
    public String test01(String name, String age, Map<String, Object> map){

        map.put("message", name + "-" + age);
        return "test";
    }

这种方式,就是将请求参数作为方法参数,SpringBoot会自动匹配完成数据的填入,我们只需要直接使用方法参数,就可以使用请求参数了。但是,这种方式仅适用于Get请求,运行效果如下

第二,通过HttpServletRequest来获取前端页面参数,适用get和post方式。

例如请求:http://localhost:8080/test02?name=zhang&age=30
    @RequestMapping("/test02")
    public String test02(HttpServletRequest request, Map<String, Object> map){

        String name = request.getParameter("name");
        String age = request.getParameter("age");
        map.put("message", name + "-" + age);
        return "test";
    }

我们可以看出,SpringBoot集成了SpringMVC注解的优点,方法参数可以自定义,这个对我们帮助很大。使用 HttpServletRequest 对象可以获取到用户提交的任何数据,都在期API方法里面。

第三,通过建立一个请求数据对象来获取参数,适用get和 post方式。

例如请求:http://localhost:8080/test03?name=zhang&age=30
package com.demo;

public class TestRequest {

    public String name;
    public int age;

    // 省略get/set方法
}

大家注意到了,请求数据对象里面的属性变量就是请求参数,两者保持一致。

    @RequestMapping("/test03")
    public String test03(TestRequest request, Map<String, Object> map){

        String name = request.getName();
        int age = request.getAge();
        map.put("message", name + "-" + age);
        return "test";
    }

这种方式与第一种差不多,但是支持post请求,不过需要我们手动定义请求数据对象。

对于请求参数比较多的情况下,推荐这种方式(尤其是post请求方式)。

第四,用注解@RequestParam获取请求参数,仅适用get方式。

例如请求:http://localhost:8080/test04?name=zhang&age=30
    @RequestMapping("/test04")
    public String test04(@RequestParam("name") String paramName,
                         @RequestParam("age") String paramAge,
                         Map<String, Object> map){

        map.put("message", paramName + "-" + paramAge);
        return "test";
    }

这种方式还不如第一种简单,但是它可以解决请求参数名和方法参数名不一致的问题。

第五,通过PathVariable注解来获取请求路径的参数,仅适用get方式。

例如请求:http://localhost:8080/test06/zhang/30

请注意,我们的请求URL不一样了,这个称之为PathInfo模式的URL。

    @RequestMapping("/test05/{name}/{age}")
    public String test06(@PathVariable("name") String paramName,
                         @PathVariable("age") String paramAge,
                         Map<String, Object> map){

        map.put("message", paramName + "-" + paramAge);
        return "test";
    }

这种方式需要保持url的名称和注解里面的名称保持一致即可。

第六,用注解@RequestBody获取请求参数,仅适用post方式。

@RequestBody主要用来接收前端传递给后端的json字符串中的数据,并且一个请求只有一个RequestBody。后期我们会介绍这种方式。

标签:03,String,age,boot,name,test,请求,SpringBoot
From: https://blog.csdn.net/richieandndsc/article/details/142912851

相关文章

  • 第04章 SpringBoot集成JDBC
    首先,我们新创建一个“SpringBootJdbcDemo”的Maven工程。然后我们修改编码格式以及Maven仓库地址,我们省略这个过程了。接着我们再添加spring-boot-starter-parent,spring-boot-starter-web,spring-boot-starter-thymeleaf依赖库,然后我们还需要添加本章节要学习的spring-bo......
  • 慧通教育C++测试题 103667--103673(5题)
    103667.求最大值难度:1登录//103667.求最大值难度:1#include<bits/stdc++.h>usingnamespacestd;intans=0,n,m,q,a[105][105];intmain(){ cin>>n>>m>>q; for(inti=1;i<=n;i++){ for(intj=1;j<=m;j++){ cin>>a[i][j]; } } int......
  • 097基于java ssm springboot汽车配件销售商城管理系统(源码+文档+运行视频+讲解视频)
    项目技术:Springboot+Maven+Vue等等组成,B/S模式+Maven管理等等。环境需要1.运行环境:最好是javajdk1.8,我们在这个平台上运行的。其他版本理论上也可以。2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;3.tomcat环境:Tomcat7.x,8.x,9.x版本均可4.硬件环境:windows......
  • 100基于java ssm springboot体检预约系统体检套餐报告体检论坛(源码+文档+运行视频+讲
    项目技术:Springboot+Maven+Vue等等组成,B/S模式+Maven管理等等。环境需要1.运行环境:最好是javajdk1.8,我们在这个平台上运行的。其他版本理论上也可以。2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;3.tomcat环境:Tomcat7.x,8.x,9.x版本均可4.硬件环境:windows......
  • 101基于java ssm springboot协同过滤算法高考志愿填报系统(源码+文档+运行视频+讲解视
    项目技术:Springboot+Maven+Vue等等组成,B/S模式+Maven管理等等。环境需要1.运行环境:最好是javajdk1.8,我们在这个平台上运行的。其他版本理论上也可以。2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;3.tomcat环境:Tomcat7.x,8.x,9.x版本均可4.硬件环境:windows......
  • 091基于java ssm springboot考研互助平台系统招生信息交流互动(源码+文档+运行视频+讲
    项目技术:Springboot+Maven+Vue等等组成,B/S模式+Maven管理等等。环境需要1.运行环境:最好是javajdk1.8,我们在这个平台上运行的。其他版本理论上也可以。2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;3.tomcat环境:Tomcat7.x,8.x,9.x版本均可4.硬件环境:windows......
  • 403错误跳到网站首页
    处理403错误(禁止访问错误)并跳转到网站首页,可以通过几种不同的方式来实现,具体取决于您使用的编程语言和框架。下面我将分别给出几种常见Web开发环境下的示例:1.PHP在PHP中,可以使用header()函数来重定向页面:<?phpif($user_not_authorized){header('HTTP/1.1403Forbi......
  • 03-第一中值定理、微积分基本定理、牛莱公式、泰勒公式(转)
    一、第一中值定理如果函数f(x)在闭区间[a,b]上连续,则在积分区间[a,b]上至少存在一个点ξξ,使得∫baf(x)dx=f(ξ)(b−a).(a⩽ξ⩽b)∫abf(x)dx=f(ξ)(b−a).(a⩽ξ⩽b)二、微积分基本定理积分上限函数:函数f(x)在区间[a,b]上连续,对于定积分∫xaf(x)dx∫axf(x)dx每一个取值的x......
  • 基于springboot的校园智能垃圾分类平台网站系统java项目
    该校园智能垃圾分类平台网站系统基于SpringBoot构建,致力于提升校园垃圾分类的效率和准确性,促进校园环境的可持续发展。对于学生和教职工来说,系统提供了便捷的使用界面。用户可以在平台上快速查询各种垃圾的分类信息,通过输入垃圾名称或描述,系统能准确告知其所属类别及正......
  • 基于微信小程序+springboot的校园综合服务系统网站java项目
    该校园综合服务系统网站项目结合微信小程序和SpringBoot技术,为校园师生提供便捷、全面的服务。对于学生而言,微信小程序端提供了丰富的功能。学生可以通过小程序查看课程安排、考试时间和成绩等学业信息,方便及时了解自己的学习进度。同时,能够在线进行图书馆书籍借阅预约......