1. 使用 RequestParam 绑定请求参数值(重要)
1.1. 处理器 SpringMVCTest 添加
/**
* @RequestParam 来映射请求参数. value 值即请求参数的参数名 required 该参数是否必须. 默认为 true
* defaultValue 请求参数的默认值
*/
@RequestMapping(value = "/testRequestParam")
public String testRequestParam(
@RequestParam(value = "username") String un,
@RequestParam(value = "age", required = false, defaultValue = "0") int age) {
System.out.println("testRequestParam, username: " + un + ", age: "
+ age);
return SUCCESS;
}
1.2.视图 index.jsp 添加
<a href="springmvc/testRequestParam?username=atguigu&age=11">Test RequestParam</a>
1.3.运行结果
testRequestParam, username: atguigu, age: 11
2. 使用 RequestHeader 绑定请求报头的属性值(了解)
2.1. 处理器类 SpringMVCTest 添加
/**
* 了解: 映射请求头信息 用法同 @RequestParam
*/
@RequestMapping("/testRequestHeader")
public String testRequestHeader(
@RequestHeader(value = "Accept-Language") String al) {
System.out.println("testRequestHeader, Accept-Language: " + al);
return SUCCESS;
}
2.2. 视图 index.jsp 添加
<a href="springmvc/testRequestHeader">Test RequestHeader</a>
<br><br>
2.3. 运行结果
testRequestHeader, Accept-Language: zh-CN
3. 使用 @CookieValue 绑定请求中的 Cookie 值(了解)
3.1. 处理器类 SpringMVCTest 添加
/**
* 了解:
*
* @CookieValue: 映射一个 Cookie 值. 属性同 @RequestParam
*/
@RequestMapping("/testCookieValue")
public String testCookieValue(@CookieValue("JSESSIONID") String sessionId) {
System.out.println("testCookieValue: sessionId: " + sessionId);
return SUCCESS;
}
3.2. 视图 index.jsp 添加
<a href="springmvc/testCookieValue">Test CookieValue</a>
<br><br>
3.3. 运行结果
testCookieValue: sessionId: 46EC8534645C40282ABA2564A20B6900
4. 使用 POJO 对象绑定请求参数值
4.1. 创建 POJO (User 类)
package www.xq.springmvc.entities;
public class User {
private Integer id;
private String username;
private String password;
private String email;
private int age;
private Address address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password
+ ", email=" + email + ", age=" + age + ", address=" + address
+ "]";
}
}
4.2. 为测试级联属性创建 Address 类
package www.xq.springmvc.entities;
public class Address {
private String province;
private String city;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Address [province=" + province + ", city=" + city + "]";
}
}
4.3. 处理器 SpringMVCTest 中添加
/**
* Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配, 自动为该对象填充属性值。支持级联属性。
* 如:dept.deptId、dept.address.tel 等
*/
@RequestMapping("/testPojo")
public String testPojo(User user) {
System.out.println("testPojo: " + user);
return SUCCESS;
}
4.4. 视图 index.jsp 中添加
<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>
city: <input type="text" name="address.city"/>
<br>
province: <input type="text" name="address.province"/>
<br>
<input type="submit" value="Submit"/>
</form>
<br><br>
4.5. 运行结果
testPojo: User [username=a, password=123456, email=1@xq.com, age=18, address=Address [province=hubei, city=wuhan]]
5. 使用 Servlet API 作为入参
5.1. 处理器类 SpringMVCTest 添加
/**
* 可以使用 Serlvet 原生的 API 作为目标方法的参数 具体支持以下类型
*
* HttpServletRequest
* HttpServletResponse
* HttpSession
* java.security.Principal
* Locale InputStream
* OutputStream
* Reader
* Writer
* @throws IOException
*/
@RequestMapping("/testServletAPI")
public void testServletAPI(HttpServletRequest request,
HttpServletResponse response, Writer out) throws IOException {
System.out.println("testServletAPI, " + request + ", " + response);
out.write("hello springmvc");
// return SUCCESS;
}
5.2. 视图 index.jsp 添加
<a href="springmvc/testServletAPI">Test ServletAPI</a>
<br><br>
5.3. 运行结果
前台
hello springmvc
后台
testServletAPI, org.apache.catalina.connector.RequestFacade@3a195593, org.apache.catalina.connector.ResponseFacade@230fec0a