springMvc-入参对象

  • Post author:
  • Post category:其他


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

转载于:https://www.cnblogs.com/csy666/p/7039264.html