12.16 接受请求参数的值
默认方式
直接给方法入参上写一个和请求参数名相同的变量,这个接收请求参数:有值就是有值/ 没值 就是null
1 2 3 4 5 6 7
| @RequestMapping("/handle") public String handle (String username, Model model){ System.out.println(username); model.addAttribute("name",username); model.addAttribute("time",System.currentTimeMillis()); return "success"; }
|
1 2 3 4 5 6 7
| @RequestMapping("/handle01") public String handle01 (@RequestParam(value = "user",required = false,defaultValue = "未指定默认值")String username, Model model){ System.out.println(username); model.addAttribute("name",username); model.addAttribute("time",System.currentTimeMillis()); return "success"; }
|
@RequestParam(“user”)有三个参数:
value 指定要获取的参数key
required 这个参数是否是必须的
defaultValue 默认值
@RequestHeader:获取请求头中的值
- 想获取哪个请求头就获取哪一个 当然是必须存在的,如果不存在的话会报500
1 2 3 4 5 6 7
| @RequestMapping("/handle02") public String handle02 (@RequestHeader(value = "User-agent") String userAgent, Model model){ System.out.println(userAgent); model.addAttribute("name",userAgent); model.addAttribute("time",System.currentTimeMillis()); return "success"; }
|
@CookieValue:获取某个Cookie的值
1 2 3 4 5 6 7
| @RequestMapping("/handle03") public String handle03 (@CookieValue(value = "JSESSIONID") String jid, Model model){ System.out.println(jid); model.addAttribute("name",jid); model.addAttribute("time",System.currentTimeMillis()); return "success"; }
|
3B0ACA55D0E6F5B9E98C79133A8E2DB0