spring mvc-POJO傳參方式

http://www.javashuo.com/article/p-pdqjrzve-en.htmlhtml

 

有兩POJO類:java

Address.java:web

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:spring

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>

 

控制器類:app

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

 

測試:jsp

一、進入jsp頁面:ide

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

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

輸出結果:

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

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

相關文章
相關標籤/搜索