当前位置 : 主页 > 编程语言 > c++ >

SpringMVC学习笔记(三)

来源:互联网 收集:自由互联 发布时间:2021-07-03
gistfile1.txt 1.1. 使用实体类接收请求参数spring MVC 会按请求参数名和 实体类属性名进行自动匹配,自动为该对象填充属性值。支持级联属性。[java] view plain copy@RequestMapping("/pojoParams") pub
gistfile1.txt
1.1. 使用实体类接收请求参数
spring MVC 会按请求参数名和 实体类属性名进行自动匹配,自动为该对象填充属性值。支持级联属性。
[java] view plain copy
@RequestMapping("/pojoParams")  
public String pojoParams(User user){  
    System.out.println(user);  
    return "helloworld";  
}  

User.Java:
[java] view plain copy
package com.ibigsea.springmvc.model;  
  
import java.io.Serializable;  
  
public class User implements Serializable {  
  
    private static final long serialVersionUID = -5975751076183659876L;  
  
    private String name;  
    private int age;  
      
    private Department department;  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public int getAge() {  
        return age;  
    }  
  
    public void setAge(int age) {  
        this.age = age;  
    }  
  
    public Department getDepartment() {  
        return department;  
    }  
  
    public void setDepartment(Department department) {  
        this.department = department;  
    }  
  
    @Override  
    public String toString() {  
        return "User [name=" + name + ", age=" + age + ", department="  
                + department + "]";  
    }  
      
}  
Department.java :
[java] view plain copy
package com.ibigsea.springmvc.model;  
  
import java.io.Serializable;  
  
public class Department implements Serializable {  
  
    private static final long serialVersionUID = 6881984318733090395L;  
      
    private Integer id;  
    private String name;  
  
    public Integer getId() {  
        return id;  
    }  
    public void setId(Integer id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    @Override  
    public String toString() {  
        return "Department [id=" + id + ", name=" + name + "]";  
    }  
      
}  
JSP:
[java] view plain copy

PoJo param

name: age: department id : department name :
运行结果: [plain] view plain copy User [name=bigsea, age=23, department=Department [id=1, name=test]]
网友评论