@RequestBody
使用
案例
public String testRequestBody(@RequestBody String Body){
System.out.println(Body);
return "success";
}
@PathVariable
案例
@RequestMapping(path = "/testPathVariable/{id}")
public String testPathVariable(@PathVariable(value = "id") String id){
System.out.println(id);
return "success";
}
<a href="/testRequestParam/testPathVariable/10">testRequestParam</a>
测试结果
@CookieValue
使用
案例
<%-- 测试@CookieValue --%>
<a href="/testRequestParam/testCookieValue">testCookieValue</a>
@RequestMapping(path = "/testCookieValue")
public String testCookieValue(@CookieValue(name = "JSESSIONID") String cookieValue){
System.out.println(cookieValue);
return "success";
}
测试结果
@SessionAttributes
作用
在Session对象中存储键值对数据,但是需要先存在Request对象中,再添加注解@SessionAttributes(name=“键名”)
案例
<%-- 测试SessionAttribute --%>
<a href="/testRequestParam/testAddSession">添加session</a>
<a href="/testRequestParam/testGetSession">获取session</a>
<a href="/testRequestParam/testDelSession">删除session</a>
@RequestMapping(path = "/testAddSession")
public String testAddSession(Model model){
model.addAttribute("学习","SpringMVC");//添加键值对到Request对象中
return "success";
}
@RequestMapping(path = "/testGetSession")
public String testGetSession(ModelMap modelmap){
System.out.println(modelmap.get("学习"));
return "success";
}
@RequestMapping(path = "/testDelSession")
public String testDelSession(SessionStatus status){
status.setComplete();
return "success";
}
测试结果
注意:使用SessionAttributes注解时,使用Model类的addAttribute()方法添加入Request对象中,使用ModelMap类的get()方法获取,并且@SessionAttributes注解只适用于类
来源:CSDN
作者:weixin_44172800
链接:https://blog.csdn.net/weixin_44172800/article/details/104180058