Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use RequestParam annotations in SpringMVC

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to use RequestParam annotations in SpringMVC", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use RequestParam annotations in SpringMVC" article.

1. Preface

Role: mainly used for the front-end request parameters for some constraints, including parameter name mismatch, whether it is necessary, the default value!

This comment is very simple, and it should be the simplest comment in MVC besides Controller.

@ Target (ElementType.PARAMETER) @ Retention (RetentionPolicy.RUNTIME) @ Documentedpublic @ interface RequestParam {@ AliasFor ("name") String value () default "; @ AliasFor (" value ") String name () default"; boolean required () default true; String defaultValue () default ValueConstants.DEFAULT_NONE;}

Although there are actually only three of the four attributes, the name attribute and the value attribute have the same meaning, aliasing each other just like the other.

Target meta-annotation constraints this annotation can only be used on parameters, Retention meta-annotation indicates the save time of this annotation, which means that it can be loaded by JVM reflection at run time!

2. Name/value attribute

These two attributes are converted when the current backend parameters do not match, that is, the front-end-name/value-backend parameters!

Once the setting name/value is added, it means that this parameter must be passed, otherwise 400 error.

This practice is very chicken rib, mismatch directly according to the front end change on the line, a little icing on the cake.

@ RequestMapping ("/ hello2") public String get2 (@ RequestParam ("a") String args, Model model) {model.addAttribute ("value", args); return "hello";}

3. Required attribute

This property is used with name/value and is true by default.

It was found above that once name/value is set, this parameter is required, while required can be set to false to indicate that this parameter is optional!

@ RequestMapping ("/ hello3") public String get3 (@ RequestParam (name = "a", required = false) String args, Model model) {model.addAttribute ("value", args); return "hello";}

4. DefaultValue attribute

Represents the default value used by the parameter if no parameter is passed.

Generally used with required=false! If required=true must be specified, why do you need a default value?

RequestMapping ("/ hello4") public String get4 (@ RequestParam (name = "a", defaultValue = "this is the default", required = false) String args, Model model) {model.addAttribute ("value", args); return "hello";}

The above is about the content of this article on "how to use RequestParam annotations in SpringMVC". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report