您的位置:首页 > 其它

使用POJO对象绑定请求参数

2018-02-05 23:19 363 查看
1.介绍

  


2.Person.java

1 package com.spring.bean;
2
3 public class Person {
4     private String username;
5     private String password;
6     private String email;
7     private int age;
8     private Address address;
9
10     public Person() {}
11     public Person(String username, String password, String email, int agem,Address address) {
12         super();
13         this.username = username;
14         this.password = password;
15         this.email = email;
16         this.age = age;
17         this.address = address;
18     }
19     public String getUsername() {
20         return username;
21     }
22     public void setUsername(String username) {
23         this.username = username;
24     }
25     public String getPassword() {
26         return password;
27     }
28     public void setPassword(String password) {
29         this.password = password;
30     }
31     public String getEmail() {
32         return email;
33     }
34     public void setEmail(String email) {
35         this.email = email;
36     }
37     public int getAge() {
38         return age;
39     }
40     public void setAge(int age) {
41         this.age = age;
42     }
43     public Address getAddress() {
44         return address;
45     }
46     public void setAddress(Address address) {
47         this.address = address;
48     }
49     @Override
50     public String toString() {
51         return "Person [username=" + username + ", password=" + password + ", email=" + email + ", age=" + age
52                 + ", address=" + address + "]";
53     }
54
55 }


3.address.java

1 package com.spring.bean;
2
3 /**
4  * 为了验证级联属性做的bean
5  * @author dell
6  *
7  */
8 public class Address {
9     private String province;
10     private String city;
11     public Address() {}
12     public Address(String province, String city) {
13         super();
14         this.province = province;
15         this.city = city;
16     }
17     public String getProvince() {
18         return province;
19     }
20     public void setProvince(String province) {
21         this.province = province;
22     }
23     public String getCity() {
24         return city;
25     }
26     public void setCity(String city) {
27         this.city = city;
28     }
29     @Override
30     public String toString() {
31         return "Address [province=" + province + ", city=" + city + "]";
32     }
33
34 }


4.controller

1 package com.spring.it;
2
3 import org.springframework.stereotype.Controller;
4 import org.springframework.web.bind.annotation.CookieValue;
5 import org.springframework.web.bind.annotation.RequestMapping;
6
7 import com.spring.bean.Person;
8
9 @Controller
10 public class RequestParamControl {
11     @RequestMapping("/pojo")
12     public String hello(Person person)
13     {
14         System.out.println("person="+person);
15         return "success";
16     }
17 }


5.index.jsp

1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2     pageEncoding="ISO-8859-1"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7 <title>Insert title here</title>
8 </head>
9 <body>
10     <form action="pojo/" method="post">
11         username:<input type="text" name="username"/>
12         <br>
13         passwaord:<input type="password" name="password"/>
14         <br>
15         email:<input type="text" name="email"/>
16         <br>
17         age:<input type="text" name="age"/>
18         <br>
19         province:<input type="text" name="address.province">
20         <br>
21         city:<input type="text" name="address.city">
22         <br>
23         <input type="submit" value="submit"/>
24     </form>
25 </body>
26 </html>


6.效果

  


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: