Spring MVC POJO传参方式

Posted JustDo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring MVC POJO传参方式相关的知识,希望对你有一定的参考价值。

有两POJO类

Address.java

 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.java

 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页面

 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 }

 

测试:

1、进入jsp页面:

2、点击提交后,控制台输出

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

 

【总结】:这里对应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]

 

上面如果输入中文。会出现中文乱码情况

以上是关于Spring MVC POJO传参方式的主要内容,如果未能解决你的问题,请参考以下文章

Spring不同类型参数传参

(04)Spring MVC之Get方式传参访问Controller,从Controller返回json串出现菱形问号(?????)乱码,解决方法。

Spring MVC controller间跳转 重定向 传参

Spring MVC POJO入参过程分析

spring mvc怎么重定向

Spring MVC @PathVariable注解