首页 > 代码库 > 注解 @RequestParam,@RequestHeader,@CookieValue,Pojo,servlet原生API
注解 @RequestParam,@RequestHeader,@CookieValue,Pojo,servlet原生API
1.@RequestParam
我们的超链接:<a href="http://www.mamicode.com/springMvc/testRequestParam">testRequestParam</a><br/>
/**
*
* @param username
* @param age
* @return
* 1.没有加required=false,
* 这样访问会报错,如果加了的话,数据类型是int,必须加defaultValue="http://www.mamicode.com/0"
* 否则也会报错,要么将数据类型改为Integer
*/
@RequestMapping(value="http://www.mamicode.com/testRequestParam")
public String testRequestParam(@RequestParam(value="http://www.mamicode.com/username") String username,
@RequestParam(value="http://www.mamicode.com/age",required=false,defaultValue="http://www.mamicode.com/0") int age){
System.out.println("testRequestParam,"+"username: "+username
+" age"+age);
return SUCCESS;
}
2.@RequestHeader
我们的超链接:<a href="http://www.mamicode.com/springMvc/testRequestHeader">testRequestHeader</a><br/>
我们header的值可以是 请求头中的任何一个
@RequestMapping("/testRequestHeader")
public String testRequestHeader(@RequestHeader(value="http://www.mamicode.com/Accept-Encoding") String str){
System.out.println("testRequestHeader()..."+str);
return SUCCESS;
}
3.@CookieValue
我们的超链接:<a href="http://www.mamicode.com/springMvc/testCookieValue">testCookieValue</a><br/>
@RequestMapping("/testCookieValue")
public String testCookieValue(@CookieValue(value="http://www.mamicode.com/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="http://www.mamicode.com/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="http://www.mamicode.com/springMvc/testServlet">testServlet</a><br/>
注解 @RequestParam,@RequestHeader,@CookieValue,Pojo,servlet原生API