Use POJO objects to bind request parameter values under @RequestMapping

from:http://www.cnblogs.com/yy3b2007com/p/8196086.html

Spring MVC will automatically match the request parameter name and POJO attribute name, automatically fill in the attribute value for the object, and support cascading attributes.

如:address.city.dept.address.province等。

Step 1: Define Account.java, Address.java classes:

copy code
 1 package com.dx.springlearn.entities;
 2 
 3 public class Account {
 4     private String username;
 5     private String password;
 6     private Integer age;
 7     private Address address;
 8 
 9     public String getUsername() {
10         return username;
11     }
12 
13     public void setUsername(String username) {
14         this.username = username;
15     }
16 
17     public String getPassword() {
18         return password;
19     }
20 
21     public void setPassword(String password) {
22         this.password = password;
23     }
24 
25     public Integer getAge() {
26         return age;
27     }
28 
29     public void setAge(Integer age) {
30         this.age = age;
31     }
32 
33     public Address getAddress() {
34         return address;
35     }
36 
37     public void setAddress(Address address) {
38         this.address = address;
39     }
40 
41     @Override
42     public String toString() {
43         return "Account [username=" + username + ", password=" + password + ", age=" + age + ", address=" + address
44                 + "]";
45     }
46 
47 }
copy code
copy code
package com.dx.springlearn.entities;

public class Address {
    private String province;
    private String city;
    private String details;

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

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    @Override
    public String toString() {
        return "Address [province=" + province + ", city=" + city + ", details=" + details + "]";
    }

}
copy code

步骤二:在HelloWord.java控制类内添加testPojo方法:

copy code
package com.dx.springlearn.handlers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.dx.springlearn.entities.Account;

@Controller
@RequestMapping("class_requestmapping")
public class HelloWord {
    private static String SUCCESS = "success";

    @RequestMapping("/testPojo")
    public String testPojo(Account account) {
        System.out.println("testPojo: account:" + account);
        return SUCCESS;
    }
}
copy code

步骤三:在index.jsp中添加表单提交html脚本:

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

步骤四:测试

提交表单后,打印结果:

testPojo: account:Account [username=abc123, password=123456, age=28, address=Address [province=zhejiang, city=hangzhou, details=hangzhou huo che zhan]]

 



Thank you for reading, if you think reading this article is helpful to you, please click the " Recommend " button. This article welcomes everyone to reprint, but after reprinting the article, the author and the original link must be given on the article page .

Guess you like

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