美文网首页
使用POJO对象绑定参数值 Spring MVC笔记(二)

使用POJO对象绑定参数值 Spring MVC笔记(二)

作者: 强出头 | 来源:发表于2017-07-24 11:14 被阅读0次

Spring MVC 会按请求参数名和 POJO 属性名进行自动匹
配,自动为该对象填充属性值。支持级联属性。
我们先创建两个类对象,来验证级联属性的支持。
User


public class User {

    private String username;
    private String password;
    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 Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", address=" + address +
                '}';
    }



}

Address


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 + '\'' +
                '}';
    }
}

后端代码


/*
    * 使用POJO绑定参数值
    * */

    @RequestMapping(value = "testPojo",method = RequestMethod.POST)
    public String testPojo(User user){
        System.out.println("testPojo   " + user);
        return SUCCESS;
    }

前端代码


<form action="/hello/testPojo" method="post">

    username: <input type="text" name="username">
    <br>
    password: <input type="password" name="password">
    <br>
    province<input type="text" name="address.province">
    <br>
    city<input type="text" name="address.city">
    <br>
    <input type="submit" value="提交">

</form>

相关文章

网友评论

      本文标题:使用POJO对象绑定参数值 Spring MVC笔记(二)

      本文链接:https://www.haomeiwen.com/subject/xeivkxtx.html