注解 @RequestParam,@RequestHeader,@CookieValue,Pojo,servlet原生API
Posted @我爱吃西瓜~~~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了注解 @RequestParam,@RequestHeader,@CookieValue,Pojo,servlet原生API相关的知识,希望对你有一定的参考价值。
1.@RequestParam
我们的超链接:<a href="springMvc/testRequestParam">testRequestParam</a><br/>
/**
*
* @param username
* @param age
* @return
* 1.没有加required=false,
* 这样访问会报错,如果加了的话,数据类型是int,必须加defaultValue="0"
* 否则也会报错,要么将数据类型改为Integer
*/
@RequestMapping(value="/testRequestParam")
public String testRequestParam(@RequestParam(value="username") String username,
@RequestParam(value="age",required=false,defaultValue="0") int age){
System.out.println("testRequestParam,"+"username: "+username
+" age"+age);
return SUCCESS;
}
2.@RequestHeader
我们的超链接:<a href="springMvc/testRequestHeader">testRequestHeader</a><br/>
我们header的值可以是 请求头中的任何一个
@RequestMapping("/testRequestHeader")
public String testRequestHeader(@RequestHeader(value="Accept-Encoding") String str){
System.out.println("testRequestHeader()..."+str);
return SUCCESS;
}
3.@CookieValue
我们的超链接:<a href="springMvc/testCookieValue">testCookieValue</a><br/>
@RequestMapping("/testCookieValue")
public String testCookieValue(@CookieValue(value="JSESSIONID" ) String sessionId){
System.out.println("testCookieValue().... "+sessionId);
return SUCCESS;
}
4.Pojo
我们建两个类,一个User ,Address,并生成get,set方法,并生成tostring方法
public class User {
private String username;
private String password;
private String email;
private int age;
private Address address;
.......
}
public class Address {
private String province;
private String city;
.....
}
我们的表单是:
<form action="springMvc/testPojo" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
email:<input type="text" name="email"><br>
age:<input type="text" name="age"><br>
province:<input type="text" name="address.province"><br>
city: <input type="text" name="address.city"><br>
<input type="submit" value="Summit">
</form>
对应的代码:
@RequestMapping("/testPojo")
public String testPojo(User user){
System.out.println(user);
return SUCCESS;
}
5.servlet原生API
@RequestMapping("/testServlet")
public void testServlet(HttpServletRequest request,HttpServletResponse
response ,Writer out) throws IOException{
System.out.println("testServlet :"+request+": "+response);
out.write("testServlet()...");
}
我们的超链接:<a href="springMvc/testServlet">testServlet</a><br/>
以上是关于注解 @RequestParam,@RequestHeader,@CookieValue,Pojo,servlet原生API的主要内容,如果未能解决你的问题,请参考以下文章
@PathVariable注解和@RequestParam注解的区别
@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
(转)@RequestParam @RequestBody @PathVariable 等参数绑定注解详解