Spring MVC POJO傳參方式

有兩POJO類html

Address.javajava

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

 

 

User.javaweb

 1 package com.proc;
 2 
 3 public class User {
 4     
 5     private String username;
 6     private String password;
 7     private String gender;
 8     private String hobby;
 9     private Address address;
10     public String getUsername() {
11         return username;
12     }
13     public void setUsername(String username) {
14         this.username = username;
15     }
16     public String getPassword() {
17         return password;
18     }
19     public void setPassword(String password) {
20         this.password = password;
21     }
22     public String getGender() {
23         return gender;
24     }
25     public void setGender(String gender) {
26         this.gender = gender;
27     }
28     public String getHobby() {
29         return hobby;
30     }
31     public void setHobby(String hobby) {
32         this.hobby = hobby;
33     }
34     public Address getAddress() {
35         return address;
36     }
37     public void setAddress(Address address) {
38         this.address = address;
39     }
40     @Override
41     public String toString() {
42         return "User [username=" + username + ", password=" + password
43                 + ", gender=" + gender + ", hobby=" + hobby + ", address="
44                 + address + "]";
45     }
46 }

 

JSP頁面spring

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
 2 
 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 4 <html>
 5   <head>
 6 
 7   </head>
 8   
 9   <body>
10     <form action="testPOJO" method="post">
11     <table>
12         <tr>
13             <td>用戶名:</td><td><input type="text" name="username"/></td>
14         </tr>
15         <tr>
16             <td>密碼:</td><td><input type="password" name="password"/></td>
17         </tr>
18         <tr>
19             <td>性別:</td>
20             <td>
21                 <input type="radio" checked="checked" name="gender" value="male"/>22                    <input type="radio" name="gender" value="female"/>23                </td>
24         </tr>
25         <tr>
26             <td>愛好:</td>
27             <td>
28                 <input type="checkbox" name="hobby" value="tv">看電視
29                 <input type="checkbox" name="hobby" value="ball">打球
30                 <input type="checkbox" name="hobby" value="swimming">游泳
31             </td>
32         </tr>
33         <tr>
34             <td>省:</td><td><input type="text" name="address.province"/></td>
35         </tr>
36         <tr>
37             <td>市:</td><td><input type="text" name="address.city"/></td>
38         </tr>
39         <tr>
40             <td></td><td><input type="submit" value="提交"></td>
41         </tr>
42 
43        </table>
44     
45     </form>
46   </body>
47 </html>

 

控制器類:數組

 1 package com.proc;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 @Controller
 7 public class Test {
 8 
 9     @RequestMapping("/testPOJO")
10     public String testPOJO(User user){
11         System.out.println(user);
12         return "hello";
13     }
14     
15 }

 

測試:app

一、進入jsp頁面:jsp

二、點擊提交後,控制檯輸出ide

User [username=caoyc, password=123456, gender=male, hobby=tv,swimming, address=Address [province=sichuan, city=chengdu]]post

 

【總結】:這裏對應address屬性使用的是級聯屬性測試

 

POJO注入數組屬性

  上面咱們看到hobby是一個多選項。那麼能夠將hobby設爲一個String[]類型的數組。設置後咱們再次

1 @RequestMapping("/testPOJO")
2 public String testPOJO(User user){
3     System.out.println(user);
4     System.out.println(Arrays.toString(user.getHobby()));
5     return "hello";
6 }

  輸出結果:

1 User [username=caoyc, password=123456, gender=male, hobby=[Ljava.lang.String;@c3d34, address=Address [province=sichuan, city=chengdu]]
2 [tv, swimming]

 

上面若是輸入中文。會出現中文亂碼狀況

相關文章
相關標籤/搜索