Springmvc常用注解
1.RequestParam
作用:把请求中指定名称的参数给控制器中的形参赋值
属性:value:请求参数中的名称
required:请求参数中是否必须提供此参数,默认值为true
示例:
//jsp中代码:
<a href="anno/textRequestParam?name=会话">RequestParam</a>
//控制器
@Controller
@RequestMapping("/anno")
public class AnnoController {
@RequestMapping("textRequestParam")
private String textRequestParam(@RequestParam("name") String username){
System.out.println("执行了...");
System.out.println(username);
return "success";
}
}
2.RequestBody
作用:用于获取请求体内容,直接使用得到的是key=value&key=value…结构的数据
get请求体不适用
属性:required:是否必须有请求体,默认为true,get请求方式会报错,为false时,get请求得到是null
示例:
<%--jsp文件--%>
<form action="anno/textRequestBody" method="post">
姓名:<input type="text" name="name"/><br/>
年龄:<input type="text" name="age"/><br/>
<input type="submit" value="提交"/>
</form>
//方法
@RequestMapping("textRequestBody")
private String textRequestBody(@RequestBody String body){
System.out.println("执行了...");
System.out.println(body);
return "success";
}
3.PathVaribale
作用:用于绑定url中的占位符,如:请求url中/delete/{id},这个{id}就是url占位符
url支持占位符是spring3.0之后加入的,是springmvc支持rest风格URL的一个重要标志
属性:value:用于指定占位符名称
required:是否必须提供占位符
示例:
<%--jsp文件--%>
<a href="anno/textPathVaribale/10086">PathVaribale</a>
//方法
@RequestMapping("textPathVaribale/{id}")
private String textPathVaribale(@PathVariable("id") String id){
System.out.println("执行了...");
System.out.println(id);
return "success";
}
4.RequestHeader
作用:用于获取请求头
属性:value:提供消息头名称
required:是否必须有此消息头
注:一般很少用
示例:
<%--jsp文件--%>
<a href="anno/textRequestHeader">RequestHeader</a>
//方法
@RequestMapping("textRequestHeader")
private String textRequestHeader(@RequestHeader("Accept") String header){
System.out.println("执行了...");
System.out.println(header);
return "success";
}
5.CookieValue
作用:用于把指定cookie名称的值传入控制器方法参数
属性:value:指定cookie的名称
required:是否必须有此cookie
示例:
<%--jsp文件--%>
<a href="anno/textCookieValue">CookieValue</a>
//方法
@RequestMapping("textCookieValue")
private String textCookieValue(@CookieValue("JSESSIONID") String cookie){
System.out.println("执行了...");
System.out.println(cookie);
return "success";
}
6.ModelAttribute
作用:出现在方法上,表示当前方法会在控制器的方法执行之前,先执行。
出现在参数上,获取指定的数据给参数赋值
属性:value:用于获取数据的key,key可以是pojo的属性名称,也可以是map结构的key
示例:
<%--jsp文件--%>
<form action="anno/textModelAttribute" method="post">
姓名:<input type="text" name="uname"/><br/>
年龄:<input type="text" name="age"/><br/>
<input type="submit" value="提交"/>
</form>
//在方法上
@ModelAttribute
public User showUser(String uname){
System.out.println("showUser执行了.....");
User user = new User();
user.setUname(uname);
user.setAge(20);
user.setDate(new Date());
return user;
}
//出现在参数上
@ModelAttribute
public void showUser(String uname, Map<String,User> map){
System.out.println("showUser执行了.....");
User user = new User();
user.setUname(uname);
user.setAge(20);
user.setDate(new Date());
map.put("a",user);
}
//接收的方法
@RequestMapping("textModelAttribute")
private String textModelAttribute(@ModelAttribute("a") User user){
System.out.println("textModelAttribute执行了...");
System.out.println(user);
return "success";
}
7.SessionAttributes
作用:用于多次执行控制器方法间的参数共享
属性:value:用于指定存入的属性名称
type:用于指定存入的数据类型
示例:
<%--jsp文件--%>
<a href="anno/textSessionAttributes">textSessionAttributes</a>
<a href="anno/getSessionAttributes">getSessionAttributes</a>
<a href="anno/deleteSessionAttributes">deleteSessionAttributes</a>
success.jsp
<h3>返回成功</h3>
${msg}
${sessionScope}
@Controller
@RequestMapping("/anno")
@SessionAttributes("msg")
public class AnnoController {
@RequestMapping("/textSessionAttributes")
private String textSessionAttributes(Model model){
System.out.println("textSessionAttribute...");
model.addAttribute("msg","你好");
return "success";
}
@RequestMapping("/getSessionAttributes")
private String getSessionAttributes(ModelMap map){
System.out.println("getSessionAttribute...");
map.get("msg");
return "success";
}
@RequestMapping("/deleteSessionAttributes")
private String deleteSessionAttributes(SessionStatus status){
System.out.println("textSessionAttribute...");
status.setComplete();
return "success";
}
}