目录
@RequestParam 请求参数
1、org.springframework.web.bind.annotation.RequestParam 注解将浏览器请求参数绑定至控制层方法参数上。
2、@RequestParam 三个常用属性:
- (1)value:浏览器请求传入的参数名,默认会将后面的方法参数名称作为接收的参数名。
- (2)required:是否必需,默认为 true,即请求中必须包含该参数,如果没有包含,将会抛出异常(可选配置)
- (3)defaultValue:当没有传入参数时,则使用此默认值。如果设置了该值,required 将自动设为 false,无论是否配置了required,配置了什么值,都是 false(可选配置)
/**
* http://localhost:8080/get1?uid=9889
* required 属性默认为 true,此时页面必须传入 uid 参数,否则报 400 错误
*
* @param uid :value 属性的值与参数名称要相同
*/
@GetMapping("get1")
public String get1(@RequestParam(value = "uid") Integer uid) {
logger.info("get = >" + uid);
return "get = >" + uid;
}
/**
* http://localhost:8080/get2?uid=998
* http://localhost:8080/get2?uid=
* required = false:此时页面可以不传入 uid 参数时,后台 uid 为 null
*
* @param uid : value 属性的值与参数名称要相同
*/
@GetMapping("get2")
public String get2(@RequestParam(value = "uid", required = false) Integer uid) {
logger.info("get = >" + uid);
return "get = >" + uid;
}
/**
* http://localhost:8080/get3
* http://localhost:8080/get3?uid=888
* 此时页面没有传入 uid 参数时,将使用默认值 9527
*
* @param uid : value 属性的值与参数名称要相同
*/
@GetMapping("get3")
public String get3(@RequestParam(value = "uid", defaultValue = "9527") Integer uid) {
logger.info("get = >" + uid);
return "get = >" + uid;
}
/**
* http://localhost:8080/get4
* http://localhost:8080/get4?uid=666
* 平时这种方法写的最多,当页面没有传入 uid 参数时,后台 uid 此时默认为 null
*/
@GetMapping("get4")
public String get4(Integer uid) {
logger.info("get = >" + uid);
return "get = >" + uid;
}
/**
* http://localhost:8080/get5?pid=77777
* RequestParam 的 value 属性指定的是调入方传入的参数名称。
* 1、value 属性不写时,默认会将后面的方法参数名称作为接收的参数名。
* 2、value 属性指定时,使用指定的值作为接收的参数名称,然后将接收的值赋给后的方法参数
*/
@GetMapping("get5")
public String get5(@RequestParam(value = "pid") Integer uid) {
logger.info("get = >" + uid);
return "get = >" + uid;
}
src/main/java/com/wmx/controller/RequestController.java · 汪少棠/jpaTransactional - Gitee.com
@PathVariable 路径变量
1、带占位符的 URL 是 Spring3.0 新增的功能,通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中。
2、URL 中的 {xxx} 占位符可以通过 @PathVariable("xxx") 绑定到操作方法的入参中。
3、如有 url 地址: http://localhost:8080/find/1/china/2?id=100&name=zhangSan。则 @RequestParam 可以用来获取 "?" 后面的 id、name 参数,而 @PathVariable 则是获取 url 中的参数,如里面的 "1"、"china"、"2"
4、 @PathVariable 有两个常用属性 value 与 required,value 用于指定 url 中的占位符名称,required 表示占位符参数是否必须。
5、required 属性注意事项:
如果 required = false,而用户没有传这个参数,那么它会去找这个参数去掉之后的替代 url,如果发现有替代的 url,就可以处理这个请求,如果没有找到,则页面报错 404。 带占位符的 url 优先级高于其它没有占位符的 url 地址。 |
如果 required = true,则带占位符的 url 必须匹配,即使还有其它的 url 匹配未传入参数的地址,同样会报错 404. |
/**
* @PathVariable : 其中的 value 属性值对应 url 中的占位符名称,只有 value 时,可以省略 value 不写,甚至直接括弧不写
* <p>
* http://localhost:8080/wmx/movie/1/2/3 :正确
* http://localhost:8080/wmx/movie/1/2/3/:正确
* http://localhost:8080/wmx/movie/1/2/3/4:报错 404
* http://localhost:8080/wmx/movie/1/2/ :报错 404,注意 1/2/ 与 1/2/3 是不同的 url
* http://localhost:8080/wmx/movie/1/2 :报错 404
*/
@GetMapping("/wmx/movie/{type}/{region}/{order}")
public String findMovie1(@PathVariable(value = "type") Integer type,//三个 type 对应
@PathVariable("region") String region,//省略 value
@PathVariable Integer order) {//直接省略括号,最简形式
logger.info("type=" + type + ", region=" + region + ", order=" + order);
return "type=" + type + ", region=" + region + ", order=" + order;
}
/**
* post 请求与 get 请求一样
* http://localhost:8080/wmx/movie2/1/china/2 :正确
* http://localhost:8080/wmx/movie2/1/china/:报错 404,与上面是两个不同的 url
* http://localhost:8080/wmx/movie2/china/2 :报错 404
*
* @return
*/
@PostMapping("/wmx/movie2/{type}/china/{order}")
public String findMovie2(@PathVariable Integer type, @PathVariable Integer order) {
logger.info("type=" + type + ", order=" + order);
return "type=" + type + ", order=" + order;
}
/**
* http://localhost:8080/wmx/movie3/1/ 正确
* http://localhost:8080/wmx/movie3/ 正确
* "/wmx/movie3/{id}"
* * 首先明白 :/wmx/movie3/ 与 /wmx/movie3 是相同的 url ,但与 /wmx/movie3/20 是不同的 url
* required 属性:表示 url 中的参数是否可以为 null,默认为 true
* * 未设置 required = false 时,则此方法的路径必须满足 "/wmx/movie3/{id}" 的格式,否则就是 404 错误
* * 有设置 required = false 后,则 "/wmx/movie3/{id}" 中的参数 id 可以为 null,此时就会去匹配没有 id 时的格式,如 "/wmx/movie3"
* XxxMapping 可以设置多个 url 地址,当然其中的 "/wmx/movie3" 也可以在其它方法中,并不一定要在同一个方法中
*
* @param id
* @return
*/
@GetMapping(value = {"/wmx/movie3", "/wmx/movie3/{id}"})
public String findMovie3(@PathVariable(required = false) Integer id) {
logger.info("id=" + id);
return "id=" + id;
}
/**
* http://localhost:8080/wmx/movie4/110 正确
* 1、value 属性不写时,默认会将后面的方法参数名称作为接收的参数名。
* 2、value 属性指定时,使用指定的值作为接收的参数名称,然后将接收的值赋给后的方法参数
*
* @param uid
* @return
*/
@GetMapping(value = {"/wmx/movie4/{pid}"})
public String findMovie4(@PathVariable(value = "pid") Integer uid) {
logger.info("id=" + uid);
return "id=" + uid;
}
src/main/java/com/wmx/controller/RequestController.java · 汪少棠/jpaTransactional - Gitee.com