@PathVariable设置为空的问题(required=false)
参考了:http://www.imooc.com/qadetail/268268
最近学习springMVC的时候,学到@PathVariable后,发现@PathVariable有个required属性,于是将其设置为false,发现访问请求时报错。
刚开始我的代码是这样的:
1 2 3 4 5 6 |
|
后面发现上面的文章,将方法改成如下就可以了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
原因就是地址是不一样的,需要配置多个地址映射。
/**
*@PathVariable为空时指定默认值,
*可以指定多个匹配路径,然后设置参数非必要的,就可以了,例子如下
*对于下面两种URL都是可以处理的
*http://www.leixingke.com/article/timeline/leixing
*http://www.leixingke.com/article/timeline/leixing/100
*/
@RequestMapping(value = {"/timeline/{uid}", "/timeline/{uid}/{size}"})
public ModelAndView getTimeline(@PathVariable(value="uid" ) String articleUserId,
@PathVariable(value="size" , required = false) Integer size,
ModelAndView modelAndView) {
if(size == null) size = 50;
//逻辑
}