SpringMVC POJO传参方式

有两POJO类

Address.java

package com.proc;
 
 public class Address {
     private String province;
     private String city;
     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;
     }
     @Override
     public String toString() {
         return "Address [province=" + province + ", city=" + city + "]";
     }
 }

 

User.java

package com.proc;
  
public class User {
    
     private String username;
     private String password;
     private String gender;
     private String hobby;
     private Address address;
     public String getUsername() {
         return username;
     }
     public void setUsername(String username) {
         this.username = username;
     }
     public String getPassword() {
         return password;
     }
     public void setPassword(String password) {
         this.password = password;
     }
     public String getGender() {
         return gender;
     }
     public void setGender(String gender) {
         this.gender = gender;
     }
     public String getHobby() {
         return hobby;
     }
     public void setHobby(String hobby) {
         this.hobby = hobby;
     }
     public Address getAddress() {
         return address;
     }
     public void setAddress(Address address) {
         this.address = address;
     }
     @Override
     public String toString() {
         return "User [username=" + username + ", password=" + password
                 + ", gender=" + gender + ", hobby=" + hobby + ", address="
                 + address + "]";
     }
}

 

JSP页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
 
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 <html>
   <head>
 
   </head>
   
   <body>
     <form action="testPOJO" method="post">
     <table>
         <tr>
             <td>用户名:</td><td><input type="text" name="username"/></td>
         </tr>
         <tr>
             <td>密码:</td><td><input type="password" name="password"/></td>
         </tr>
         <tr>
             <td>性别:</td>
             <td>
                 <input type="radio" checked="checked" name="gender" value="male"/><input type="radio" name="gender" value="female"/></td>
         </tr>
         <tr>
             <td>爱好:</td>
             <td>
                 <input type="checkbox" name="hobby" value="tv">看电视
                 <input type="checkbox" name="hobby" value="ball">打球
                 <input type="checkbox" name="hobby" value="swimming">游泳
             </td>
         </tr>
         <tr>
             <td>省:</td><td><input type="text" name="address.province"/></td>
         </tr>
         <tr>
             <td>市:</td><td><input type="text" name="address.city"/></td>
         </tr>
         <tr>
             <td></td><td><input type="submit" value="提交"></td>
         </tr>
 
        </table>  
     </form>
   </body>
 </html>

 

 

控制器类:

package com.proc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class Test {

    @RequestMapping("/testPOJO")
    public String testPOJO(User user){
        System.out.println(user);
        return "hello";
    }
   
}

 

 

测试:

1、进入jsp页面:

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

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

 

【总结】:这里对应address属性使用的是级联属性

 

POJO注入数组属性

  上面我们看到hobby是一个多选项。那么可以将hobby设为一个String[]类型的数组。设置后我们再次

@RequestMapping("/testPOJO")
public String testPOJO(User user){
    System.out.println(user);
    System.out.println(Arrays.toString(user.getHobby()));
    return "hello";
}

 

  输出结果:

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

 

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

 

本文转自:http://www.cnblogs.com/caoyc/p/5635640.html

posted @ 2018-04-09 17:05  Margaery  阅读(228)  评论(0编辑  收藏  举报