SpringMVC中@RequestParam注解是如何实现参数接收的原理分析?

2026-05-26 07:101阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计603个文字,预计阅读时间需要3分钟。

SpringMVC中@RequestParam注解是如何实现参数接收的原理分析?

一、作用作用:在方法调用前用于传递参数,用于接收所传参数

二、说明内部的四个属性:

1.name:指定传入的参数名称

例如:javapublic void selectStudentById(String id) { // 使用参数id进行数据库查询等操作}例如:`http://localhost:8081/selectStudentById?id=01` 接收查询号后面的参数值(允许多个参数)

一、作用

作用在方法传递的参数前,用于接收所传参数

例如:localhost:8081/selectStudentById?id=01 接收问号后面的参数值(允许多个参数)

二、注解内部的四个属性

1.name

指定传入的参数名称,其后面跟的参数名称一定要与前端传入的参数名称一致

2.value

指定传入的参数名称,其后面跟的参数名称一定要与前端传入的参数名称一致

3.requred

指定参数是否是必传参数,如果不指定,默认为true

4.defaultValue

指定参数的默认值

注意:其中name和value属性的作用等同的.其源码中name的别名就是value,value的别名就是name

三、注意事项

1.@RequestParam可以解决前后端定义的参数名不一致的问题

例如前端传入的参数名是name,后端方法接收的参数名是userName,这时可以通过@RequestParam指定value的值为name,实现name与userName的映射

SpringMVC中@RequestParam注解是如何实现参数接收的原理分析?

@RequestMapping(method = RequestMethod.GET, value = "selectCourseAndTeacherByStudent") public Course selectCourseAndCourseByStudent(@RequestParam(value = "name") String userName) { Course course = studentService.selectCourseAndTeacherByStudent(userName); return course; }

2.如果后端使用的是基本数据类型来接收参数,那么一定要设置required=false,并且要设置一个默认值

@RequestMapping(method = RequestMethod.GET,value = "selectStudentById") public Student selectStudentById(@RequestParam(value = "id",required = false,defaultValue = "01") int id){ return studentService.selectStudentById(id); }

因为考虑到前端没有传值的情况,如果此时仅仅设置了required=false,会报500错误(下图异常)因为基本数据类型无法接收null,

3.如果后端使用的是引用数据类型,则无需设置required=false和defaultValue

因为即使前端没有传入参数值,引用数据类型是可以接收null的

@RequestMapping(method = RequestMethod.GET,value = "selectStudentById") public Student selectStudentById(@RequestParam(value = "id") Integer id){ return studentService.selectStudentById(id); }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计603个文字,预计阅读时间需要3分钟。

SpringMVC中@RequestParam注解是如何实现参数接收的原理分析?

一、作用作用:在方法调用前用于传递参数,用于接收所传参数

二、说明内部的四个属性:

1.name:指定传入的参数名称

例如:javapublic void selectStudentById(String id) { // 使用参数id进行数据库查询等操作}例如:`http://localhost:8081/selectStudentById?id=01` 接收查询号后面的参数值(允许多个参数)

一、作用

作用在方法传递的参数前,用于接收所传参数

例如:localhost:8081/selectStudentById?id=01 接收问号后面的参数值(允许多个参数)

二、注解内部的四个属性

1.name

指定传入的参数名称,其后面跟的参数名称一定要与前端传入的参数名称一致

2.value

指定传入的参数名称,其后面跟的参数名称一定要与前端传入的参数名称一致

3.requred

指定参数是否是必传参数,如果不指定,默认为true

4.defaultValue

指定参数的默认值

注意:其中name和value属性的作用等同的.其源码中name的别名就是value,value的别名就是name

三、注意事项

1.@RequestParam可以解决前后端定义的参数名不一致的问题

例如前端传入的参数名是name,后端方法接收的参数名是userName,这时可以通过@RequestParam指定value的值为name,实现name与userName的映射

SpringMVC中@RequestParam注解是如何实现参数接收的原理分析?

@RequestMapping(method = RequestMethod.GET, value = "selectCourseAndTeacherByStudent") public Course selectCourseAndCourseByStudent(@RequestParam(value = "name") String userName) { Course course = studentService.selectCourseAndTeacherByStudent(userName); return course; }

2.如果后端使用的是基本数据类型来接收参数,那么一定要设置required=false,并且要设置一个默认值

@RequestMapping(method = RequestMethod.GET,value = "selectStudentById") public Student selectStudentById(@RequestParam(value = "id",required = false,defaultValue = "01") int id){ return studentService.selectStudentById(id); }

因为考虑到前端没有传值的情况,如果此时仅仅设置了required=false,会报500错误(下图异常)因为基本数据类型无法接收null,

3.如果后端使用的是引用数据类型,则无需设置required=false和defaultValue

因为即使前端没有传入参数值,引用数据类型是可以接收null的

@RequestMapping(method = RequestMethod.GET,value = "selectStudentById") public Student selectStudentById(@RequestParam(value = "id") Integer id){ return studentService.selectStudentById(id); }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。