码迷,mamicode.com
首页 > 编程语言 > 详细

springMvc-入参对象

时间:2017-06-17 10:23:40      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:后台   test   inpu   也会   ext   code   super   control   pre   

 

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

springMvc-入参对象

标签:后台   test   inpu   也会   ext   code   super   control   pre   

原文地址:http://www.cnblogs.com/csy666/p/7039264.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!