springmvc常用注解
1.@controller 注解是否是控制器类
2. @requestMapping 请求路径的注解
在类和方法上都可以用
可以设置请求路径 也可以设置请求方法
@RequestMapping("/User")
@RequestMapping(value = "/login5",method = RequestMethod.POST)
3.@RequestParam 用在请求参数上
如果传递的参数名和接收的参数名不一致 需要@RequestParam 注解
3.1 如果传递的参数名和接收的参数名一致
<a href="User/login2?name=aa&pwd=123">登录2</a><br/>
@RequestMapping("/login2")
public String login2(String name,String pwd)
{
System.out.println(name);
System.out.println(pwd);
return "success";
}
3.1 如果传递的参数名和接收的参数名不一致
<a href="User/login4?uname=lisi&upwd=123">登录4</a><br/>
@RequestMapping("/login4")
public String login4(@RequestParam(value="uname") String name, @RequestParam(value="upwd") String pwd)
{
System.out.println(name);
System.out.println(pwd);
return "success";
}
4.@PathVariable
在路径使用rest风格的时候 需要用@PathVariable注解路径上的参数
<a href="User/login3/zhangsan/123">登录3</a><br/>
//rest风格
@RequestMapping("/login3/{aa}/{pwd}")
public String login3(@PathVariable(value="aa") String name,@PathVariable(value="pwd") String pwd)
{
System.out.println(name);
System.out.println(pwd);
return "success";
}
5.@RequestHeader
请求头
<a href="User/testHeader2">testHeader2</a><br/>
@RequestMapping("/testHeader2")
public String testHeader2(@RequestHeader("host") String header)
{
System.out.print(header);
return "success";
}
6.@CookieValue
获取cookie中的值
<a href="User/setCookie">存cookie</a><br/>
<a href="User/getCookie">取cookie</a>
@RequestMapping("/setCookie")标签:RequestMapping,常用,String,SpringMVC,System,pwd,cookie,注解,out From: https://blog.51cto.com/u_15707781/5764706
public String setCookie(HttpServletResponse response)
{
Cookie cookie=new Cookie("uname","zhangsan");
cookie.setMaxAge(3600);
cookie.setPath("/");
response.addCookie(cookie);
return "success";
}
@RequestMapping("/getCookie")
public String getCookie(@CookieValue("uname") String uname)
{
System.out.println(uname);
return "success";
}