SpringMVC系列(四)使用 POJO 對象綁定請求參數值


在實際開發中如果參數太多就不能使用@RequestParam去一個一個的映射了,需要定義一個實體參數對象(POJO)來映射請求參數。Spring MVC 會按請求參數名和 POJO 屬性名進行自動匹配,自動為該對象填充屬性值。支持級聯屬性如:address.province、address.city等。

具體步驟如下:

1.定義需要的請求參數的實體

User.java

 1 package com.study.springmvc.model;
 2 
 3 public class User {
 4     
 5     private String username;
 6     private String password;
 7 
 8     private String email;
 9     private int age;
10     
11     private Address address;
12 
13     public String getUsername() {
14         return username;
15     }
16 
17     public void setUsername(String username) {
18         this.username = username;
19     }
20 
21     public String getPassword() {
22         return password;
23     }
24 
25     public void setPassword(String password) {
26         this.password = password;
27     }
28 
29     public String getEmail() {
30         return email;
31     }
32 
33     public void setEmail(String email) {
34         this.email = email;
35     }
36 
37     public int getAge() {
38         return age;
39     }
40 
41     public void setAge(int age) {
42         this.age = age;
43     }
44 
45     public Address getAddress() {
46         return address;
47     }
48 
49     public void setAddress(Address address) {
50         this.address = address;
51     }
52 
53     @Override
54     public String toString() {
55         return "User [username=" + username + ", password=" + password + ", email=" + email + ", age=" + age
56                 + ", address=" + address + "]";
57     }
58 
59 }

Address.java

 1 package com.study.springmvc.model;
 2 
 3 public class Address {
 4 
 5     private String province;
 6     private String city;
 7 
 8     public String getProvince() {
 9         return province;
10     }
11 
12     public void setProvince(String province) {
13         this.province = province;
14     }
15 
16     public String getCity() {
17         return city;
18     }
19 
20     public void setCity(String city) {
21         this.city = city;
22     }
23 
24     @Override
25     public String toString() {
26         return "Address [province=" + province + ", city=" + city + "]";
27     }
28     
29 }

2. 在index.jsp頁面編寫傳參的表單

 1 <!--POJO測試 begin  --> 
 2 <br>
 3 <form action="pojoTest/testPojo" method="post">
 4     username: <input type="text" name="username"/>
 5     <br>
 6     password: <input type="password" name="password"/>
 7     <br>
 8     email: <input type="text" name="email"/>
 9     <br>
10     age: <input type="text" name="age"/>
11     <br>
12     city: <input type="text" name="address.city"/>
13     <br>
14     province: <input type="text" name="address.province"/>
15     <br>
16     <input type="submit" value="Submit"/>
17 </form>
18 <!--POJO測試 end  --> 

3.編寫handler

PojoTest.java

 1 package com.study.springmvc.handlers;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 import com.study.springmvc.model.User;
 7 
 8 @RequestMapping("/pojoTest")
 9 @Controller
10 public class PojoTest {
11 
12     public static final String SUCCESS="success";
13     
14     /**
15      * Spring MVC 會按請求參數名和 POJO 屬性名進行自動匹配,
16      * 自動為該對象填充屬性值。支持級聯屬性。
17      * 如:address.province、address.city等。
18      */
19     @RequestMapping("/testPojo")
20     public String testPojo(User user) {
21         System.out.println("testPojo: " + user);
22         return SUCCESS;
23     }
24 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM