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]

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值