- 地址栏参数或普通表单请求,如:age=18&gender=man,SpringMVC方法参数需要使用@RequestParam(“参数名”)获得参数值。
- 提交json类型数据,SpringMVC 方法参数需要使用@RequestBody 将数据封装到java对象中。
- 路径占位符:如 http://localhost:8080/user/findById/1,使用@RequestMapping(“findById/{id}”)匹配路径,使用@Path Variable(“id”)获得对应的参数值。
- 文件上传,修改表单请求为"multipart/form-data"方式,SpringMVC采用MultipartFile参数类型接收数据
package com.czxy.controller;
import com.czxy.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping("/param")
public class ParamController {
@GetMapping
public void demo01(@RequestParam("username") String username, @RequestParam("password") String password){
}
@PostMapping
public void demo02(@RequestBody User user){
}
@GetMapping("/{uid}")
public void demo03(@PathVariable("uid") Integer uid){
}
@PostMapping("/upload")
public void demo04(MultipartFile image){
}
}