@RequestParam注解,加与不加的区别
@RequestParam
-
将
请求参数
绑定到控制器方法参数
上 -
SpringMVC提供的接收普通参数的注解
源码:
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
boolean required() default true;
String defaultValue() default "nttnttnue000ue001ue002nttttn";
}
@RequestParam(value="参数名",required="true/false",defaultValue="")
/*
* value:参数名
* required:设置是否包含该参数,默认是true,表示该请求路径中必须包含该参数,如果不包含就会报400错误
* defaultValue:设置默认参数值,如果设置了该值,required=true就会失效,自动为false,如果没有传该参数,就使用默认值
/
加与不加
使用不使用该注解,都可以获取参数,但二者还是有区别的。
-
不使用@RequestParam注解时,
- 请求路径中不跟 参数,获取的参数为null;
-
使用@RequestParam 注解时,
-
请求路径中不跟 参数,页面会报错,返回400
-
如果设置了required=“false”,就和不使用@RequestParam注解一样了
-
https://mp.weixin.qq.com/s/xs-NPRENNtVPn_7z7m9Xxw