SpringMVC 6. Binding request parameter values using POJO objects

Bind request parameter values ​​using POJO objects

SpringMVC will automatically match the request parameter name and POJO attribute name, and automatically fill in the attribute value for the object. Cascading properties are supported. Such as: address.provice, user.address.tel, etc.

cotroller class:

@RequestMapping("/springmvc")
@Controller
public class TestPOJO {
    public static final String SUCCESS = "success" ;

    @RequestMapping(value = "/testPOJO")
    public String testPOJO(User user){
        System.out.println("testPOJO:"+ user);
        System.out.println("testPOJO");
        return SUCCESS ;
    }
}

entities class

  • User.java
public class User {
    private String username;
    private String password;
    private Integer age;
    private String email;

    private Address address;

    public User(String username, String password, Integer age, String email) {
        this.username = username;
        this.password = password;
        this.age = age;
        this.email = email;
    }

    @Override
    public String toString() {
        return "User{" +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                ", email='" + email + '\'' +
                ", 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 Integer getAge() {
        return age;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Address getAddress() {
        return address;
    }

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


    public User() {
    }

    public User(String username, String password, Integer age, String email) {

        this.username = username;
        this.password = password;
        this.age = age;
        this.email = email;
    }
}
  • Address .java
public class Address {
    private String city ;
    private String provice ;

    public String getCity() {
        return city;
    }

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

    public String getProvice() {
        return provice;
    }

    public void setProvice(String provice) {
        this.provice = provice;
    }

    @Override
    public String toString() {
        return "Address{" +
                "city='" + city + '\'' +
                ", provice='" + provice + '\'' +
                '}';
    }
}

jsp page:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>POJO</title>
</head>
<body>
<form action="/springmvc/testPOJO" method="post">
    <br>
    username:<input type="text" name="username">
    <br>
    password:<input type="password" name="password">
    <br>
    age:<input type="text" name="age">
    <br>
    email:<input type="text" name="email">
    <br>
    city:<input type="text" name="address.city">
    <br>
    provice:<input type="text" name="address.provice">
    <br>
    <input type="submit" value="submit">

</form>
</body>
</html>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325410377&siteId=291194637