SpringMVC学习 -- 使用 POJO 对象绑定请求参数值
Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配 , 自动为该对象填充属性值 , 支持级联属性。如:address.province。
1 package com.itdoc.springmvc.entities; 2 3 /** 4 * @BLOG http://www.cnblogs.com/goodcheap 5 * @DESCRIBE 6 * @AUTHOR WángChéngDá 7 * @DATE 2017-03-09 11:35 8 */ 9 public class User { 10 11 private String username; 12 13 private int age; 14 15 private String password; 16 17 private String email; 18 19 private Address address; 20 21 public String getUsername() { 22 return username; 23 } 24 25 public void setUsername(String username) { 26 this.username = username; 27 } 28 29 public int getAge() { 30 return age; 31 } 32 33 public void setAge(int age) { 34 this.age = age; 35 } 36 37 public String getPassword() { 38 return password; 39 } 40 41 public void setPassword(String password) { 42 this.password = password; 43 } 44 45 public String getEmail() { 46 return email; 47 } 48 49 public void setEmail(String email) { 50 this.email = email; 51 } 52 53 public Address getAddress() { 54 return address; 55 } 56 57 public void setAddress(Address address) { 58 this.address = address; 59 } 60 61 @Override 62 public String toString() { 63 return "User{" + 64 "username='" + username + '\'' + 65 ", age=" + age + 66 ", password='" + password + '\'' + 67 ", email='" + email + '\'' + 68 ", address=" + address + 69 '}'; 70 } 71 }
1 package com.itdoc.springmvc.entities; 2 3 /** 4 * @BLOG http://www.cnblogs.com/goodcheap 5 * @DESCRIBE 6 * @AUTHOR WángChéngDá 7 * @DATE 2017-03-09 11:37 8 */ 9 public class Address { 10 11 private String province; 12 13 private String city; 14 15 public String getProvince() { 16 return province; 17 } 18 19 public void setProvince(String province) { 20 this.province = province; 21 } 22 23 public String getCity() { 24 return city; 25 } 26 27 public void setCity(String city) { 28 this.city = city; 29 } 30 31 @Override 32 public String toString() { 33 return "Address{" + 34 "province='" + province + '\'' + 35 ", city='" + city + '\'' + 36 '}'; 37 } 38 }
1 package com.itdoc.springmvc.entities; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RequestMethod; 6 7 /** 8 * @BLOG http://www.cnblogs.com/goodcheap 9 * @DESCRIBE 10 * @AUTHOR WángChéngDá 11 * @DATE 2017-03-09 11:38 12 */ 13 @Controller 14 public class TestPojo { 15 16 public static final String SUCCESS = "success"; 17 18 @RequestMapping(value = "/getPojo", method = RequestMethod.POST) 19 public String getPojo(User user) { 20 System.out.println(user); 21 return SUCCESS; 22 } 23 }
1 <%-- 2 User: WángChéngDá 3 Date: 2017-03-08 4 Time: 11:40 5 --%> 6 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 7 <html> 8 <head> 9 <title>$Title$</title> 10 </head> 11 <form action="/getPojo" method="post"> 12 username:<input type="text" name="username"/> 13 <br> 14 password:<input type="password" name="password"/> 15 <br> 16 age:<input type="text" name="age"/> 17 <br> 18 email:<input type="text" name="email"/> 19 <br> 20 province:<input type="text" name="address.province"/> 21 <br> 22 city:<input type="text" name="address.city"/> 23 <br> 24 <input type="submit" value="submit"/> 25 </form> 26 </body> 27 </html>