当前位置:网站首页>@RequestParam注解的详细介绍
@RequestParam注解的详细介绍
2022-07-30 00:19:00 【小前端而已】
@RequestParam (org.springframework.web.bind.annotation.RequestParam)用于将指定的请求参数赋值给方法中的形参。
有三个属性:
(1)value:请求参数名(必须配置)
(2)required:是否必需,默认为 true,即 请求中必须包含该参数,如果没有包含,将会抛出异常(可选配置)
(3)defaultValue:默认值,如果设置了该值,required 将自动设为 false,无论你是否配置了required,配置了什么值,都是 false(可选配置)
1为演示效果,首先需要创建一个测试控制类,然后创建一个测试方法,_如图所示,_____方法里添加一个参数,____此时方法参数没有添加注解

通过idea工具启动web项目,然后通过浏览器访问测试效果,不带参数则获取的参数为null,但页面可以正常显示,请求可以正常的执行。

带上参数之后,参数可以正常的获取并打印出来了

下面来对比下添加注解的效果,但只是添加@RequestParam,没有带任何的注解设置参数。

再次通过浏览器访问查看效果,此时带上userName参数页面可以正常显示并打印出结果值,但如图所示,不带参数的时候,页面报错,返回400,并提示userName不存在


当然这样的限制是可以选择的,@RequestParam里添加required=false来关闭必须参数的限制,这样就和第一种不带注解的效果一样了


@RequestParam还可以在参数为空的情况下,为我们设置一个参数的默认值,如图所示,可以看出来,没有参数的情况下,获取到的就是默认值

默认参数的key名称是方法里的参数名称,但通过@RequestParam可以自定义另外一个,因为在某些条件下,方法名称固定下来不可以修改了,但请求的参数又需要通过另外一个参数key名称来传递,这样就可以通过这样的方式来实现


边栏推荐
- 更换可执行文件glibc版本的某一次挣扎
- Google Chrome (google) is set to translate Chinese, the translation option does not take effect or the translation option does not pop up
- [Training DAY16] ALFA [convex hull] [computational geometry]
- From the perspective: the interviewer interview function test engineer mainly inspects what ability?
- Expansion of Parallel I/O Port in Single Chip Microcomputer Development
- Graph Theory: Bipartite Graphs
- 定时器学习
- 中文语义匹配
- Recurrent Neural Network (RNN)
- 4 hotspot inquiry networks necessary for new media operations
猜你喜欢

NumPy(一)

BEVDetNet: Bird's Eye View LiDAR Point Cloud based Real-time 3D Object Detection for Autonomous Drivi

Douyin short video traffic acquisition strategy, mastering these will definitely be a hit

自媒体人如何打造出爆文?这3种类型的文章最容易爆

rk-boot framework combat (1)

How to increase account weight?3 ways to operate your own media to help you get more revenue

Worthington Dissociation Enzymes: Collagenase and Four Basic Profiles

MySQL 用 BETWEEN AND 日期查询包含范围边界

利用热点事件来创作软文的3大技巧?自媒体人必看

月薪15k的阿里测试岗,面试原来这么简单
随机推荐
字符串替换空格
Comprehensive Practice - Three-Mison Chess Mini Game
【分层强化学习】survey
Worthington用于细胞收获的胰蛋白酶&细胞释放程序
Override and customize dependent native Bean methods
Laravel 预防 SQL 注入
Chinese semantic matching
First Normal Form, Second Normal Form, Third Normal Form
新媒体运营必备的4个热点查询网
关于 byte 的范围
Getting Started with Sentinel
2022年ps应该选择哪个版本
Reading notes. This is the psychology: see through the essence of the pseudo psychology (version 10)"
UE4 makes crosshair + recoil
servlet执行详解
I.MX6U-驱动开发-3-新字符驱动
Introduction to Worthington Elastase & Hyaluronidase
Worthington dissociating enzyme: detailed analysis of neutral protease (dispase)
How to increase account weight?3 ways to operate your own media to help you get more revenue
Add, delete, modify and query the database