springMvc-入參對象


 

1.修改或者添加對象

2.多添件查詢時候也會遇到

springMvc能夠根據屬性自動的封裝pojo的對象並且支持關聯的對象:大致的原理是在傳入后台的時候把前台的屬性和對象封裝成json的形式傳入后台,后台根據傳入的對象,把Json的形式轉換為對象進行處理

具體使用:

-1.地址實體類:包含省份和城市倆個屬性

package com.atguigu.springmvc.entity;

public class Adress {
    private String privence;
    private String city;
    public String getPrivence() {
        return privence;
    }
    public void setPrivence(String privence) {
        this.privence = privence;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    @Override
    public String toString() {
        return "Adress [privence=" + privence + ", city=" + city + ", getPrivence()=" + getPrivence() + ", getCity()=" + getCity() + ", getClass()="
                + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
    }
    
    
}

-2.創建用戶實體:包含用戶名,年齡和關聯對象地址

package com.atguigu.springmvc.entity;

public class User {
    
    private String name;
    private String age;
    private Adress adress;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public Adress getAdress() {
        return adress;
    }
    public void setAdress(Adress adress) {
        this.adress = adress;
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + "]";
    }
    
    
    
}

-3.在頁面創建表單,包含用戶信息

<form action="springmvc/testPojo" method="post">
        name:<input type="text" name="name" value=""/>
        age:<input type="text" name="age" value=""/>
        city:<input type="text" name="adress.city" value=""/>
        <input type="submit" value="提交PUT"/>
    </form>

-4.在controller中進行處理

@RequestMapping(value="/testPojo")
    public String testPojo(User user){
        return SUCCESS;
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM