当请求的参数名称与Controller的业务方法不一致时,就需要通过@RequestParam注解进行显示的绑定

1.value:映射参数

@RequestMapping("/report17")
@ResponseBody //将name映射到username
public void save17(@RequestParam(value = "name") String username){
System.out.println(username);
}

启动服务器进行访问http://localhost:8080/user/report17?name=zsh

SpringMVC-注解@RequestParam_springmvc


其他具体代码请访问该链接


2.required:默认是true,意味着当你访问时必须带name=xxx这个数据,如果不带就会报错

SpringMVC-注解@RequestParam_注解_02


当我修改required=false时

@RequestMapping("/report17")
@ResponseBody
public void save17(@RequestParam(value = "name",required = false) String username){
System.out.println(username);
}

启动服务器进行访问不加参数值就不再报错

SpringMVC-注解@RequestParam_springmvc_03

SpringMVC-注解@RequestParam_数据_04


3.defaultValue:当你没有指定参数时,则使用指定默认值

@RequestMapping("/report17")
@ResponseBody
public void save17(@RequestParam(value = "name",required = false,defaultValue = "mo") String username){
System.out.println(username);
}

启动服务器访问

SpringMVC-注解@RequestParam_springmvc_05


SpringMVC-注解@RequestParam_启动服务_06

这里使用了defaultValue指定的默认值