方式一:传统request接收
//param01?id=12
@RequestMapping("/param01")
public String param01(HttpServletRequest request){
String id = request.getParameter("id");
System.out.println("id="+id);
return "success";
}
方式二:直接接收参数
//param02?id=12
@RequestMapping("/param02")
public String param02(int id){
System.out.println("id="+id);
return "success";
}
方式三:数组接收参数
//param03?likes=aa&likes=bb
@RequestMapping("/param03")
public String param03(String[] likes){
System.out.println("likes="+ Arrays.toString(likes));
return "success";
}
方式四:集合接收参数
//param04?likes=aa&likes=bb
@RequestMapping("/param04")
public String param04(@RequestParam("likes")List<String>likes){
System.out.println("likes="+ likes);
return "success";
}
方式五:日期接收参数
方法一:字符串接收
//param05?date=2024-08-27
@RequestMapping("/param05")
public String param05(String date) throws ParseException {
System.out.println("date="+ date);
//将字符串转化为日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse(date);
System.out.println(date1);
return "success";
}
方法二:形式参数直接接收
这种方式对日期有严格要求,yyyy/MM/dd
//param15?date=2024/08/27
@RequestMapping("/param15")
public String param15(Date date){
System.out.println("date="+ date);
//将字符串转化为日期
return "success";
}
方法三:初始化绑定
//param25?date=2024/08/27
@InitBinder
public void initData(WebDataBinder wdb){
wdb.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
@RequestMapping("/param25")
public String param25(Date date){
System.out.println("date="+ date);
//将字符串转化为日期
return "success";
}
方式六:RestFull风格传参
//param06/name
@RequestMapping("/param06/{name}")
public String param06(@PathVariable String name){
System.out.println("name="+ name);
return "success";
}
方式七:对象传参
@RequestMapping("/param07")
public String param07(User user){
System.out.println("user="+ user);
return "success";
}