当前位置:网站首页>Detailed introduction of @RequestParam annotation
Detailed introduction of @RequestParam annotation
2022-07-30 00:34:00 【Just a small front end】
@RequestParam (org.springframework.web.bind.annotation.RequestParam) is used to assign the specified request parameter to the formal parameter in the method.
There are three properties:
(1) value: request parameter name (must be configured)
(2) required: Whether it is required, the default is true, that is, this parameter must be included in the request, if not included, an exception will be thrown (optional configuration)
(3) defaultValue: default value, if this value is set, required will be automatically set to false, no matter whether you configure required or not, and what value is configured, it is false (optional configuration)
1 In order to demonstrate the effect, first you need to create a test control class, and then create a test method, _As shown in the figure, add a parameter to the _____ method, ____At this time, the method parameterNo annotations added

Start the web project through the idea tool, and then access the test effect through the browser. If there is no parameter, the obtained parameter is null, but the page can be displayed normally and the request can be executed normally.

After adding the parameters, the parameters can be normal

Let's compare the effect of adding annotations, but just add @RequestParam without any annotations to set parameters.

View the effect through the browser again. At this time, the page with the userName parameter can be displayed normally and the result value can be printed out, but as shown in the figure, when the parameter is not used, the page reports an error, returns 400, and promptsuserName does not exist


Of course, such restrictions are optional. Add required=false to @RequestParam to close the restrictions on required parameters, which is the same as the first one without annotations


@RequestParam can also set a default value of a parameter for us when the parameter is empty, as shown in the figure, it can be seen that if there is no parameter, the obtained value is the default value
p>The key name of the default parameter is the parameter name in the method, but another one can be customized through @RequestParam, because under certain conditions, the method name cannot be modified after being fixed, but the requested parameters need to bePass another parameter key name, so it can be implemented in this way



边栏推荐
猜你喜欢

“灯塔工厂”的中国路径:智造从点到面铺开
![[Training DAY16] ALFA [convex hull] [computational geometry]](/img/26/ecc77dabdf468b3a8ad3888b292496.png)
[Training DAY16] ALFA [convex hull] [computational geometry]

自媒体短视频怎么提高播放量?从这三个方面入手

Go日志库——logrus

月薪15k的阿里测试岗,面试原来这么简单

Selenium上传文件有多少种方式?不信你有我全

『牛客|每日一题』走迷宫

KDE Frameworks 5.20.0: Plasma welcomes many improvements

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

canvas 中如何实现物体的框选(六)
随机推荐
Replace the executable file glibc version of the one
The strongest JVM in the whole network is coming!(Extreme Collector's Edition)
Ubuntu中使用SQLite
KDE Frameworks 5.20.0:Plasma迎来诸多改进
try_catch捕获异常
Print linked list from end to beginning
Worthington解离酶:中性蛋白酶(分散酶)详情解析
Based on TNEWS 'today's headline news in Chinese short text classification
Chinese semantic matching
工厂模式
string replace spaces
docker安装redis集群(含部署脚本)
账号权重怎么提升?自媒体运营的3个方法,帮你获得更多收益
Linux-安装MySQL(详细教程)
Laravel 预防 SQL 注入
排序相关应用
"The lighthouse factory" of China path: smart roll out from point to surface
vim相关介绍(二)
百度智能云章淼:详解企业级七层负载均衡开源软件BFE
Docker installs redis cluster (including deployment script)