SpringMVC(七):@RequestMapping下使用POJO对象绑定请求参数值
Spring MVC会按照请求参数名和POJO属性名进行自动匹配,自动为该对象填充属性值,支持级联属性。
如:address.city.dept.address.province等。
步骤一:定义Account.java,Address.java类:
1 package com.dx.springlearn.entities; 2 3 public class Account { 4 private String username; 5 private String password; 6 private Integer age; 7 private Address address; 8 9 public String getUsername() { 10 return username; 11 } 12 13 public void setUsername(String username) { 14 this.username = username; 15 } 16 17 public String getPassword() { 18 return password; 19 } 20 21 public void setPassword(String password) { 22 this.password = password; 23 } 24 25 public Integer getAge() { 26 return age; 27 } 28 29 public void setAge(Integer age) { 30 this.age = age; 31 } 32 33 public Address getAddress() { 34 return address; 35 } 36 37 public void setAddress(Address address) { 38 this.address = address; 39 } 40 41 @Override 42 public String toString() { 43 return "Account [username=" + username + ", password=" + password + ", age=" + age + ", address=" + address 44 + "]"; 45 } 46 47 }
package com.dx.springlearn.entities; public class Address { private String province; private String city; private String details; 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; } public String getDetails() { return details; } public void setDetails(String details) { this.details = details; } @Override public String toString() { return "Address [province=" + province + ", city=" + city + ", details=" + details + "]"; } }
步骤二:在HelloWord.java控制类内添加testPojo方法:
package com.dx.springlearn.handlers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import com.dx.springlearn.entities.Account; @Controller @RequestMapping("class_requestmapping") public class HelloWord { private static String SUCCESS = "success"; @RequestMapping("/testPojo") public String testPojo(Account account) { System.out.println("testPojo: account:" + account); return SUCCESS; } }
步骤三:在index.jsp中添加表单提交html脚本:
<form name="testPojo" method="POST" action="class_requestmapping/testPojo"> username:<input type="text" name="username" /> <br> password:<input type="password" name="password" /> <br> age:<input type="text" name="age" /> <br> address:<br> province:<input type="text" name="address.province" /> <br> city:<input type="text" name="address.city" /> <br> details:<input type="text" name="address.details" /> <br> <input type="submit" value="Submit"> </form> <br>
步骤四:测试
提交表单后,打印结果:
testPojo: account:Account [username=abc123, password=123456, age=28, address=Address [province=zhejiang, city=hangzhou, details=hangzhou huo che zhan]]
基础才是编程人员应该深入研究的问题,比如:
1)List/Set/Map内部组成原理|区别
2)mysql索引存储结构&如何调优/b-tree特点、计算复杂度及影响复杂度的因素。。。
3)JVM运行组成与原理及调优
4)Java类加载器运行原理
5)Java中GC过程原理|使用的回收算法原理
6)Redis中hash一致性实现及与hash其他区别
7)Java多线程、线程池开发、管理Lock与Synchroined区别
8)Spring IOC/AOP 原理;加载过程的。。。
【+加关注】。