首页 > 编程语言 > 详细

SpringMVC数据绑定

时间:2020-05-13 19:09:07      阅读:65      评论:0      收藏:0      [点我收藏+]

在前后端交互时,前端需要向后端传递参数,后端接收参数并处理请求,而参数的类型有

基本数据类型(int、double、Interger)

对象类型 (String、自定义实体类)

日期类型(Date)

复杂类型(数组、List、Set、Map等)

特殊类型(JSON、XML)

基本类型

url中的参数名需要和方法的参数名保持一致,也可以通过@RequestParam修改url中的参数名称

因为当url参数不传递时,形参会默认转为null,所以当形参为基本数据类型时 ,如果不传递或者传递为null,会报错提示无法转为null值,但当形参味基本数据包装类型时,则不会报错,因为它可以转为null

@RestController
public class TestController {
  	//http://localhost:8080/test 报错提示but cannot be translated into a null value due to being declared as a primitive type
    @GetMapping("/test")
    public String test(int num) {
        return num + "";
    }
  
   //http://localhost:8080/test 返回null
  	@GetMapping("/test")
    public String test(int num) {
        return num + "";
    }
}

对象类型

自定义实体类Student和Teacher:

public class Student{
    String name;
    int age;
}
public class Teacher{
    String name;
    int age;
    Student student;
}

单层对象:当实体类中属性的类型为基本数据类型时,当不传递也不会报错,会转为基本数据类型的默认值,当时如果传递null,就会报错提示无法转换

//单层对象
//http://localhost:8080/test2?num=1  age不传不会报错,会转为默认值0
//http://localhost:8080/test2?num=1&age=null 会报错,因为int类型和null无法转换
@GetMapping("/test2")
public String test2(User user) {
    return user.toString();
}

多层对象:当传递多层对象时,内层对象的参数格式为对象名.属性名

http://localhost:8080/test3?name=zhangsan&age=12&student.name=wangwu&student.age=1
@GetMapping("/test3")
public String test3(Teacher teacher) {
    return teacher.toString();
}

多个对象同属性名:当传递相同对象属性名相同时,一个是通过命名去规范尽量不要同名,一个是使用@InitBinder注解为对象声明不同的前缀,传递时,参数名前加上前缀用于区分

@InitBinder("teacher")
public void initBinder1(WebDataBinder binder){
    binder.setFieldDefaultPrefix("teacher.");
}
@InitBinder("student")
public void initBinder2(WebDataBinder binder){
    binder.setFieldDefaultPrefix("student.");
}
//http://localhost:8080/test4?student.name=zs&teacher.name=ls;这里的student与teacher与上面的@InitBinder的Prefix相对应
@GetMapping("/test4")
public String test4(Student student, Teacher teacher) {
    return student.toString() + "===" + teacher.toString();
}

复杂类型

SpringMVC对于复杂类型的支持不是很好,对于List、Map、Set这种类型需要单独设置一个包装类将List、Map包起来

创建一个包装类:

public class StudentList {
    private List<Student> studentList; 
}

传递数组:url参数名对应形参数组名

//http://localhost:8080/test5?ids=1&ids=2&ids=3
@GetMapping("/test5")
public void test5(String[] ids) {
    for (String id : ids) {
        System.out.println(id);
    }
}

传递List:url参数名对应包装类的List的属性名,参数格式:如果List是对象集合格式为list属性名[下标].对象属性名,如果是String类型list属性名[下标]

http://localhost:8080/test6?studentList[0].name=123&studentList[0].age=456
@GetMapping("/test6")
public String test6(StudentList studentList) {
    return studentList.toString();
}

特殊类型

SpringMVC对于JSON支持很好,也是常用的前后端传输格式,使用注解@RequestBody接收Json格式参数,通过@ResponseBody响应json格式数据

只有post请求才可以使用,因为是将数据放在请求体传输get请求没有请求体

{
  "name":"张三",
  "age":"25"
}
@PostMapping("/test7")
public String test7(@RequestBody Student student) {
    return student.toString();
}

RESTful风格

RESTful API 风格,通过资源定位的方式来请求,每一个URI代表一个资源,将参数拼接在URI上

//普通方式:
//URL:http://localhost:8080/test8?id=123
@PostMapping("/test8")
public String test8(Integer id) {
    return id + "";
}
//RESTful方式
//URL:http://localhost:8080/test9/123
@PostMapping("/test9/{id}")
public String test9(@PathVariable Integer id) {
    return id + "";
}

SpringMVC数据绑定

原文:https://www.cnblogs.com/dingjn/p/12884044.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!