@RequestParam注解

2024-05-09 16:18
文章标签 注解 requestparam

本文主要是介绍@RequestParam注解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spring MVC 通过分析处理方法的签名,将 HTTP 请求信息绑定到处理方法的相应人参中。

Spring MVC 对控制器处理方法签名的限制是很宽松的,几乎可以按喜欢的任何方式对方法进行签名。

必要时可以对方法及方法入参标注相应的注解(@PathVariable、 @RequestParam、 @RequestHeader 等)、 SpringMVC 框架会将 HTTP 请求的信息绑定到相应的方法入参中,并根据方法的返回值类型做出相应的后续处理。

在处理方法入参处使用 @RequestParam 可以把请求参数传递给请求方法

– value:参数名

– required:是否必须。默认为 true, 表示请求参数中必须包含对应的参数,若不存在,将抛出异常


控制器类和处理函数如下:

package com.happyBKs.springmvc.handlers;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;@RequestMapping("class")
@Controller
public class RPTestHandler {String page="successrm";@RequestMapping("student")public String handle(@RequestParam(value="username") String un, @RequestParam(value="age") int age){System.out.println("a student's request has come. username: "+un+", age: "+age);return page;}}

这里使用@RequestParam注解的value属性值来映射请求参数。

请求页面index8.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="class/student?username=happyBKs&age=100">class/student?username=happyBKs&age=100</a>
</body>
</html>

好,运行过程如下:

点击超拦截请求:

控制台此时输出:

a student's request has come. username: happyBKs, age: 100


也可以直接在浏览器地址栏输入http://localhost:8080/mymvc/class/student?username=happyBKs&age=101

控制台显示   a student's request has come. username: happyBKs, age: 101



问题1:如果我们的请求少一个参数age,会怎么样?

这个问题该如何解决呢?

这里需要用到@RequestParam注解的required属性或defaultValue属性。

required属性标注这个参数是否是必需大的,默认是true,如果想让它可以不存在,那么就设置为false。但是请注意,required设置成false的参数对应的处理函数的参数类型必须是对象类型,否则回报错500!

例如这里我们如果将控制器类改成:

package com.happyBKs.springmvc.handlers;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;@RequestMapping("class")
@Controller
public class RPTestHandler {String page="successrm";@RequestMapping("student")public String handle(@RequestParam(value="username") String un, @RequestParam(value="age",required=false) int age){System.out.println("a student's request has come. username: "+un+", age: "+age);return page;}}

结果会报错,因为age虽然设置了required为false,请求参数可以不包含age,但是在处理函数中对应的参数类型是int,int是基本数据类型,无法为空,所以报错。解决办法是将int改为Integer。

运行结果:


控制台输出:

a student's request has come. username: happyBKs, age: null



如果我们仍然想用基本类型作为参数类型,那么可以用到@RequestParam注解的defaultValue属性来指定默认值。

package com.happyBKs.springmvc.handlers;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;@RequestMapping("class")
@Controller
public class RPTestHandler {String page="successrm";@RequestMapping("student")public String handle(@RequestParam(value="username") String un, @RequestParam(value="age",required=false, defaultValue="0") int age){System.out.println("a student's request has come. username: "+un+", age: "+age);return page;}}

运行结果:


控制台 a student's request has come. username: happyBKs, age: 0


最后,做个总结:(@RequestParam注解是很常用的,所以很重要)

 * @RequestParam 来映射请求参数

 * value值 即请求参数名

 * required 该参数是否必需。默认为true

 * defaultValue请求参数的默认值



这篇关于@RequestParam注解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/973869

相关文章

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

Idea实现接口的方法上无法添加@Override注解的解决方案

《Idea实现接口的方法上无法添加@Override注解的解决方案》文章介绍了在IDEA中实现接口方法时无法添加@Override注解的问题及其解决方法,主要步骤包括更改项目结构中的Languagel... 目录Idea实现接China编程口的方法上无法添加@javascriptOverride注解错误原因解决方

Java中基于注解的代码生成工具MapStruct映射使用详解

《Java中基于注解的代码生成工具MapStruct映射使用详解》MapStruct作为一个基于注解的代码生成工具,为我们提供了一种更加优雅、高效的解决方案,本文主要为大家介绍了它的具体使用,感兴趣... 目录介绍优缺点优点缺点核心注解及详细使用语法说明@Mapper@Mapping@Mappings@Co

Java中注解与元数据示例详解

《Java中注解与元数据示例详解》Java注解和元数据是编程中重要的概念,用于描述程序元素的属性和用途,:本文主要介绍Java中注解与元数据的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参... 目录一、引言二、元数据的概念2.1 定义2.2 作用三、Java 注解的基础3.1 注解的定义3.2 内

SpringBoot使用注解集成Redis缓存的示例代码

《SpringBoot使用注解集成Redis缓存的示例代码》:本文主要介绍在SpringBoot中使用注解集成Redis缓存的步骤,包括添加依赖、创建相关配置类、需要缓存数据的类(Tes... 目录一、创建 Caching 配置类二、创建需要缓存数据的类三、测试方法Spring Boot 熟悉后,集成一个外

使用@Slf4j注解,log.info()无法使用问题

《使用@Slf4j注解,log.info()无法使用问题》在使用Lombok的@Slf4j注解打印日志时遇到问题,通过降低Lombok版本(从1.18.x降至1.16.10)解决了问题... 目录@Slf4androidj注解,log.info()无法使用问题最后解决总结@Slf4j注解,log.info(

spring—使用注解配置Bean

从Spring2.5开始,出现了注解装配JavaBean的新方式。注解可以减少代码的开发量,spring提供了丰富的注解功能,现在项目中注解的方式使用的也越来越多了。   ** 开启注解扫描          Spring容器默认是禁用注解配置的。打开注解扫描的方式主要有两种: <context:component-scan>组件扫描和<context:annotation

Spring Boot 注解探秘:HTTP 请求的魅力之旅

在SpringBoot应用开发中,处理Http请求是一项基础且重要的任务。Spring Boot通过提供一系列丰富的注解极大地简化了这一过程,使得定义请求处理器和路由变得更加直观与便捷。这些注解不仅帮助开发者清晰地定义不同类型的HTTP请求如何被处理,同时也提升了代码的可读性和维护性。 一、@RequestMapping @RequestMapping用于将特定的HTTP请求映射到特定的方法上

Redis缓存 自定义注解+aspect+反射技术实现

最近再给云随笔后台增加redis模块,突然发现spring-boot-starter-data-redis模块很不人性化,实现不了通用的方式,(当然,你也可以自己写个通用的CacheUtil来实现通用的方式),但由于本人非常的爱装逼,就在这里不讲解那种傻瓜式操作了,这里只讲干货,干到你不可置信的干货). 例如:这里我使用了它其中的RedisTemplate ,发现存到redis中后,数据