使用pojo对象绑定请求参数

  • Spring MVC 会按请求参数名和POJO属性名自动匹配,自动为该对象填充属性值,支持级联属性
    如:dept.deptid、dept.address.tel等
//user类

public class User {
    
    private String name;
    
    private String password;
    
    private Integer age;

    private Address address;
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User [name=" + name + ", password=" + password + ", age=" + age + ", address=" + address + "]";
    }
}
//address类
public class Address {
    
    private String city;
    
    private String street;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    @Override
    public String toString() {
        return "Address [city=" + city + ", street=" + street + "]";
    }
    
    
    
}
    //控制器
    @Controller
    @RequestMapping("/testpojo")
    public class pojo {
        @RequestMapping("/pojo")
        public String testPojo(User user) {
          System.out.println(user);
          return "success";
        }
    } 

    //前端视图
    
姓名:
密码:
年龄:
市:
街道:

你可能感兴趣的:(使用pojo对象绑定请求参数)