Column【SpringMVC】
Favorite verse: Heaven moves vigorously, and a gentleman strives to constantly strive for self-improvement.
Music Sharing【As You Wish】
Welcome and thank everyone for pointing out Xiaoji’s problem
Article directory
- Common parameters
- POJO parameters
- Nested pojo parameters
- Array parameters
- Collection parameters
- json data parameter transfer
-
- json format
- Nested json format
- Collection format
In Web project development, how to obtain the parameters passed by the client is a very important function. SpringMVC provides a comprehensive and flexible request parameter binding mechanism, which greatly simplifies parameter processing.
This article will comprehensively introduce the parameter binding function of SpringMVC. We will learn the configuration methods to obtain basic type parameters, POJO parameters, array parameters, and collection parameters. SpringMVC automatically maps the request parameters to the input parameter object of the processing method, avoiding the tedious process of manually parsing the request.
What’s more powerful is that SpringMVC also provides automatic conversion support for JSON data. We only need to use the @RequestBody annotation to parse the JSON parameters into the target object. This low-intrusive processing method makes interface development extremely smooth.
This article will also introduce support for parameter conversion, formatting, and verification, making parameter binding richer. These knowledge points are widely used in actual projects and are the basis for mastering SpringMVC. Let’s study in depth and become even more powerful!
We continue to use the code from the previous article “Exploring the request mapping path, Get request and Post request”
Common parameters
We write this piece of code in the UserController class
@RequestMapping("/commonParamDifferentname") @ResponseBody public String commonParamDifferentName(@RequestParam("name") String userName,int age){<!-- --> System.out.println("Normal parameter passing userName ==> " + userName); System.out.println("Normal parameter passing age ==> " + age); return "{'module':'common param different name'}"; }
Received successfully
POJO parameters
We write this piece of code in the User class
package com.example.domain; public class User {<!-- --> private String name; private int age; private Address address; @Override public String toString() {<!-- --> return "User{" + "name='" + name + '\'' + ", age=" + age + ", address=" + address + '}'; } public Address getAddress() {<!-- --> return address; } public void setAddress(Address address) {<!-- --> this.address = address; } public String getName() {<!-- --> return name; } public void setName(String name) {<!-- --> this.name = name; } public int getAge() {<!-- --> return age; } public void setAge(int age) {<!-- --> this.age = age; } } </code><img class="look-more-preCode contentImg-no-view" src="//i2.wp.com/csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreBlack. png" alt="" title="">
We write this piece of code in the UserController class
//POJO parameters: The request parameters correspond to the attributes in the formal parameter object to complete the parameter transfer. @RequestMapping("/pojoParam") @ResponseBody public String pojoParam(User user){<!-- --> System.out.println("pojo parameter passing user ==> " + user); return "{'module':'pojo param'}"; }
Run successfully
Nested pojo parameters
Applicable to the following, can be written repeatedly
We write this piece of code in the Address class
package com.example.domain; public class Address {<!-- --> private String province; private String city; @Override public String toString() {<!-- --> return "Address{" + "province='" + province + '\'' + ", city='" + city + '\'' + '}'; } public String getProvince() {<!-- --> return province; } public void setProvince(String province) {<!-- --> this.province = province; } public String getCity() {<!-- --> return city; } public void setCity(String city) {<!-- --> this.city = city; } } </code><img class="look-more-preCode contentImg-no-view" src="//i2.wp.com/csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreBlack. png" alt="" title="">
Write this code in UserController
@RequestMapping("/pojoContainPojoParam") @ResponseBody public String pojoContainPojoParam(User user){<!-- --> System.out.println("pojo nested pojo parameter passing user ==> " + user); return "{'module':'pojo contain pojo param'}"; }
Continue testing
Run successfully
Array parameters
@RequestMapping("/arrayParam") @ResponseBody public String arrayParam(String[] likes){<!-- --> System.out.println("Array parameter transfer likes ==> " + Arrays.toString(likes)); return "{'module':'array param'}"; }
Run successfully
Collection parameters
@RequestMapping("/listParam") @ResponseBody public String listParam(@RequestParam List<String> likes){<!-- --> System.out.println("Set parameter transfer likes ==> " + likes); return "{'module':'list param'}"; }
Run successfully
json data parameter transmission
We add this code @EnableWebMvc
to SpringMvcConfig to enable automatic conversion of json data
json format
We find the code for the collection parameters above and change @RequestParam to @RequestBody
@RequestMapping("/listParamForJson") @ResponseBody public String listParamForJson(@RequestBody List<String> likes){<!-- --> System.out.println("list common(json) parameter passing list ==> " + likes); return "{'module':'list common for json param'}"; }
Select json here and write the data below
Found that it can run successfully
Nested json format
@RequestMapping("/pojoParamForJson") @ResponseBody public String pojoParamForJson(@RequestBody User user){<!-- --> System.out.println("pojo(json) parameter passing user ==> " + user); return "{'module':'pojo for json param'}"; }
Run successfully
Collection format
@RequestMapping("/listPojoParamForJson") @ResponseBody public String listPojoParamForJson(@RequestBody List<User> list){<!-- --> System.out.println("list pojo(json) parameter passing list ==> " + list); return "{'module':'list pojo for json param'}"; }
If an error is reported, just restart it and it will be fine
Run successfully
Through the study of this article, we have mastered various methods of parameter acquisition and binding in SpringMVC, which will help us easily implement parameter transfer of the interface. Of course, in practical applications, we must also pay attention to the optimization of safety, efficiency, standardization and other aspects.
SpringMVC parameter binding is just the tip of the iceberg. There are also data verification, global processing, interceptors and other knowledge points that need to be studied in depth. In addition, actual projects may also need to deal with complex business scenarios. If you have any questions, please leave a message with me in the comment area to discuss. With persistence, we will surely become the masters of SpringMVC!