zoukankan      html  css  js  c++  java
  • SpringMVC系列(四)使用 POJO 对象绑定请求参数值

    在实际开发中如果参数太多就不能使用@RequestParam去一个一个的映射了,需要定义一个实体参数对象(POJO)来映射请求参数。Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配,自动为该对象填充属性值。支持级联属性如:address.province、address.city等。

    具体步骤如下:

    1.定义需要的请求参数的实体

    User.java

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

    Address.java

     1 package com.study.springmvc.model;
     2 
     3 public class Address {
     4 
     5     private String province;
     6     private String city;
     7 
     8     public String getProvince() {
     9         return province;
    10     }
    11 
    12     public void setProvince(String province) {
    13         this.province = province;
    14     }
    15 
    16     public String getCity() {
    17         return city;
    18     }
    19 
    20     public void setCity(String city) {
    21         this.city = city;
    22     }
    23 
    24     @Override
    25     public String toString() {
    26         return "Address [province=" + province + ", city=" + city + "]";
    27     }
    28     
    29 }

    2. 在index.jsp页面编写传参的表单

     1 <!--POJO测试 begin  --> 
     2 <br>
     3 <form action="pojoTest/testPojo" method="post">
     4     username: <input type="text" name="username"/>
     5     <br>
     6     password: <input type="password" name="password"/>
     7     <br>
     8     email: <input type="text" name="email"/>
     9     <br>
    10     age: <input type="text" name="age"/>
    11     <br>
    12     city: <input type="text" name="address.city"/>
    13     <br>
    14     province: <input type="text" name="address.province"/>
    15     <br>
    16     <input type="submit" value="Submit"/>
    17 </form>
    18 <!--POJO测试 end  --> 

    3.编写handler

    PojoTest.java

     1 package com.study.springmvc.handlers;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 
     6 import com.study.springmvc.model.User;
     7 
     8 @RequestMapping("/pojoTest")
     9 @Controller
    10 public class PojoTest {
    11 
    12     public static final String SUCCESS="success";
    13     
    14     /**
    15      * Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配,
    16      * 自动为该对象填充属性值。支持级联属性。
    17      * 如:address.province、address.city等。
    18      */
    19     @RequestMapping("/testPojo")
    20     public String testPojo(User user) {
    21         System.out.println("testPojo: " + user);
    22         return SUCCESS;
    23     }
    24 }
  • 相关阅读:
    lsblk命令详解
    lspci命令详解
    numastat命令详解
    lsscsi命令详解
    lscpu命令详解
    linux内核模块相关命令:lsmod,depmod,modprobe,modinfo,insmod,rmmod 使用说明
    elk收集tomcat的日志
    npm安装
    centos7.5 解决缺少libstdc++.so.6库的原因及解决办法
    linux下正确卸载rpm包
  • 原文地址:https://www.cnblogs.com/leeSmall/p/7818349.html
Copyright © 2011-2022 走看看