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/>