1. Environmental preparation
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope> //确定范围避免与tomcat冲突
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId> //启动spring内的webmvc
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
<uriEncoding>UTF-8</uriEncoding> //此处是解决tomcat中文乱码问题
</configuration>
</plugin>
</plugins>
</build>
2. Request
1.Set the mapping path
Optimized: As you can see, both the user and book classes have save() methods, so @RequestMapping() should be added to the class to distinguish the two classes.
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/save")
@ResponseBody
public String save(){
System.out.println("user save ...");
return "{'module':'user save'}";
}
@RequestMapping("/delete")
@ResponseBody
public String save(){
System.out.println("user delete ...");
return "{'module':'user delete'}";
}
}
@Controller
@RequestMapping("/book")
public class BookController {
@RequestMapping("/save")
@ResponseBody
public String save(){
System.out.println("book save ...");
return "{'module':'book save'}";
}
}
2. Chinese garbled characters appear in the get request
Add <uriEncoding>UTF-8</uriEncoding> of <configuration> tag in environment preparation
3. The post request is garbled.
Post requests need to configure filters in ServletContainersInitConfig
4. Five request parameters
(1).General parameters
Expand applications:
The name defined in @RequestParam("name") can use username as name to pass parameters.
(2).POJO data type
In fact, it is to pass the object
(3). Nested POJO type parameters
(4).Array type parameters
(5). Collection type parameters
Collection type parameters must be decorated with @RequestParam when accepting formal parameters.
Reason: SpringMVC treats List as a POJO object, creates an object from it, and prepares to encapsulate front-end data into the object. However, List is an interface and cannot create an object, so an error is reported.
Knowledge points
5.JSON data transmission parameters
Common json data formats
(1).JSON ordinary array
(2).JSON object data
(3).JSON object array
Knowledge points
6. Passing date type parameters
Note : The default string-to-date format supported by SpringMVC is yyyy/MM/dd
If you want to receive parameters in different formats, you can do this:
These conversions are done by SpringMVC by calling the Converter interface