1 //使用RESTful风格传送参数 2 @RequestMapping("/item/{itemId}") 3 @ResponseBody //该注解是将返回的对象转成json格式的数据响应 4 //注解@PathVariable表示接受占位符传递过来的值 5 public TbItem queryItemById(@PathVariable Long itemId){ 6 return itemService.queryItemById(itemId); 7 }
6、@RequestParam的使用:
第一种方式:@RequestParam(defaultValue="1")表示设置controller方法上形参的默认值,通常用于在分页时设置当前页数的默认值为1,因为页面第一次访问时当前页数page变量的值null。
1 /** 2 * 分页查询广告内容表中的数据: 3 * @param page:当前页数 4 * @param rows:每页显示的记录数 5 * @param categoryId:默认展示内容类目为0的记录 6 * @return 7 */ 8 @RequestMapping("/query/list") 9 @ResponseBody 10 //@RequestParam(defaultValue="1")表示默认当前页数为第一页 11 public DatagridResult queryContentList(@RequestParam(defaultValue="1") Integer page,Integer rows,Long categoryId){ 12 13 return contentService.queryContentList(page, rows, categoryId); 14 }
第二种方式:@RequestParam(value="id",defaultValue="0")表示设置controller方法上形参变量的值是通过value属性的变量(id)赋值的,通过defaultValue属性设置默认值。