来看几个例子 , 秒懂 , 很简单
java
//http://localhost/param5?username=zhangsan&age=18
@GetMapping("/param5")
public String param5(@RequestParam Map<String,String> map){
map.forEach((k,v)->{
System.out.println(k+"==>"+v);
});
return "/index.jsp";
}
//http://localhost/param4?hobby=zq&hobby=pq&hobby=tq
@GetMapping("/param4")
public String param4(@RequestParam List<String> hobby){
for (String s : hobby) {
System.out.println(s);
}
return "/index.jsp";
}
//http://localhost/param3?hobby=zq&hobby=pq&hobby=tq
@GetMapping("/param3")
public String param3(String[] hobby){
for (String s : hobby) {
System.out.println(s);
}
return "/index.jsp";
}
//http://localhost/param2?username=zhangsan&age=18
@GetMapping("/param2")
public String param2(@RequestParam(value="username",required = false,defaultValue = "haohao") String name, Integer age){
System.out.println(name+"===="+age);
return "/index.jsp";
}
//http://localhost/param1?username=zhangsan&age=18
@RequestMapping("/param1")
public String param1(String username,int age){
System.out.println(username+"===="+age);
return "/index.jsp";
}
来看第二种方式
用一个user类封装信息 , 很方便 ,(SpringMVC容器已经帮我们把User对象创建了)
java
//http://localhost/param6?username=zhangsan&age=18&hobbies=zq&hobbies=pq&birthday=2018/11/11&address.city=tj&address.area=bh
@GetMapping("/param6")
public String param6(User user){
System.out.println(user);
return "/index.jsp";
}