springmvc对象作为 目标方法的参数。

/**
     * Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配, 自动为该对象填充属性值。支持级联属性。
     * 如:dept.deptId、dept.address.tel 等
     */
    @RequestMapping("/testPojo")
    public String testPojo(User user){
        System.out.println(user);
        return SUCCESS;
    }

user对象:

package com.springmvc.entities;

public class User {
    private String username;
    private Integer age;
    private Address address;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    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 [username=" + username + ", age=" + age + ", address="
                + address + "]";
    }
    
}

Address对象

package com.springmvc.entities;

public class Address {
    private String city;
    private String country;
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    @Override
    public String toString() {
        return "Address [city=" + city + ", country=" + country + "]";
    }
    
}

 

posted @ 2017-12-13 01:01  陆伟  阅读(251)  评论(0编辑  收藏  举报