SpringMVC POJO傳參方式

有兩POJO類html

Address.javajava

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

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頁面spring

<%@ 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";
    }
   
}

 

 

測試:app

一、進入jsp頁面:jsp

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

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

 

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

 

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

相關文章
相關標籤/搜索