目录
@RequestParam
用于和传递的参数名称进行绑定
比如form表单向controller层发送请求:
<form th:action="@{/testParam}" method="get">
用户名:<input type="text" name="user_name"><br>
密码:<input type="password" name="password"><br>
爱好:<input type="checkbox" name="hobby" value="a">a
<input type="checkbox" name="hobby" value="b">b
<input type="checkbox" name="hobby" value="c">c
<input type="submit" value="get方式提交">
</form>
@RequestMapping("/testParam")
public String testParam(
@RequestParam(value = "user_name",
required = false,/*设置为false就是收不到参数也不会报错*/
defaultValue = "")/*如果接收不到参数,这里设置个默认值*/
String username,
String password,
String[] hobby){
System.out.println("username:"+username+",password:"+password+"爱好:"+ Arrays.toString(hobby));
return "success";
}
如果用户名的name属性设置为user_name,而参数是username,即使名字不一样,用RequestParam进行绑定仍然可以接收到参数,如果不进行绑定便会报错。
@RequestHeader
- 用于请求头和参数进行绑定映射,像普通的参数进行请求时会有默认的绑定,即使不进行参数绑定它也会自动通过名字去找数据,除非请求的数据名字和形参名字不同时会接收不到参数。而请求头没有默认的绑定,想要获取请求头必须进行绑定
<form th:action="@{/testParam}" method="get"> 用户名:<input type="text" name="user_name"><br> 密码:<input type="password" name="password"><br> 爱好:<input type="checkbox" name="hobby" value="a">a <input type="checkbox" name="hobby" value="b">b <input type="checkbox" name="hobby" value="c">c <input type="submit" value="get方式提交"> </form>
@RequestMapping("/testParam") public String testParam( @RequestParam(value = "user_name", required = false,/*设置为false就是收不到参数也不会报错*/ defaultValue = "")/*如果接收不到参数,这里设置个默认值*/String username, String password, String[] hobby @RequestHeader("Host")String host){ System.out.println("username:"+username+",password:"+password+"爱好:"+ Arrays.toString(hobby)); System.out.println(host); return "success"; }
进行请求头绑定之后便能成功输出请求头localhost:8080
@Pathvariable
用在形参上面,来接收前端传过来的路径变量。
前端:<a href="/car/1/owner/yangyang">发送请求</a>//路径里面的变量可以传到后端被接受
后端;@GetMapping("/car/{id}/owner/{username]")//用{id}代表路径变量的名字
public Map<string,Object> getCar(@PathVariable("id") Integer id, //获取到的id值赋给形参
@PathVariable("username") String name,
@PathVariable Map<.String,String> pv//把路径中的所有变量存到这个集合中,输出形式为”id“:1,”username“:“yangyang”
) {
Map<.String,Object> map=new HashMap<>();//新建一个map集合存储获取到的数据
map.put("id" ,id);
map.put("username" ,name);
}
@RequestAttribute
此注解用于获取request请求中的数据
@GetMapping("、goto")//接收/goto请求,并转发到/success请求
public String goToPage(HttpServletRequest request){
request.setAttribute(s:"msg",o:"成功了。。。");//往request中放入数据
request.setAttribute(s:"code",o:200);
return "forward/sucess";
}
@ResponseBody
@GetMapping("/success")
public Map success(@RequestAttribute("msg") String msg,
@RequestAttribute("code") Integer code,//这两个都是通过注解直接获取到值给形参
@HttpServletRequest request//原生request
){
Object msg1=request.getAttribute(s:"msg");
Map<String,Object> map=new HashMap<>();
map.put("reqMethod_msg",msg1);//把原生方式获取到的值放进集合
map.put("annotation_msg",msg);//把注解方式获取到的值放入集合
return map;
}
@MatrixVariable矩阵变量
有一个请求:/cars/sell;low=34;brand=byd,audi
public Map carsSell(@MatrixVariable("low") Integer low,
@MatrixVariable("brand") List<String> brand){
Map<String,Object> map=new Hashmap<>();
map.put("low",low);
map.put("brand",brand);
return map;
}