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

springmvc之使用POJO作为参数

时间:2020-01-10 22:36:56      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:poj   dex   cti   etag   request   cte   服务   rri   inpu   

springmvc会按请求参数名和POJO属性名进行匹配,自动为该对象填充属性值,支持级联属性。

User.java

package com.gong.springmvc.entities;

public class User {
    private String username;
    private String password;
    private String email;
    private Integer age;
    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 String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    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 + ", password=" + password + ", email=" + email + ", age=" + age
                + ", address=" + address + "]";
    }
    
    
}

Address.java

package com.gong.springmvc.entities;

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 + "]";
    }
    
}

SpringmvcTest.java

import com.gong.springmvc.entities.User;

@RequestMapping("/springmvc")
@Controller
public class SpringmvcTest {
    private static final String SUCCESS = "success";
    
    @RequestMapping(value="/testPojo")
    public String testPojo(User user) {
        System.out.println("testPojo:"+user);
        return SUCCESS;
    }
}

index.jsp

    <form action="springmvc/testPojo" method="POST"><br>
        <span>username:</span><input type="text" name="username"><br>
        <span>password:</span><input type="password" name="password"><br>
        <span>email:</span><input type="text" name="password"><br>
        <span>age:</span><input type="text" name="password"><br>
        <span>city:</span><input type="text" name="address.city"><br>
        <span>street:</span><input type="text" name="address.street"><br>
        <input type="submit" value="submit"><br>
    </form>

启动tomcat服务器之后:

技术图片

 

输入相应信息点击提交:

技术图片技术图片

 

成功为pojo赋值,包括级联属性address。 

springmvc之使用POJO作为参数

标签:poj   dex   cti   etag   request   cte   服务   rri   inpu   

原文地址:https://www.cnblogs.com/xiximayou/p/12178067.html

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