`

@RequestParam注解介绍

阅读更多

1、在处理方法入参处使用@RequestParam可以把请求参数传递给请求方法,其属性如下:
(1)value:表示请求参数的参数名。
(2)required:表示请求参数是否必须。默认值为 true, 表示请求参数中必须包含对应的参数,若不存在,将抛出异常。
(3)defaultValue:表示请求参数的默认值。

 

2、控制器TestRequestMappingController.java

package com.springmvc.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/springmvc")
public class TestRequestMappingController {

	@RequestMapping("/testRequestParam")
	public String testRequestParam(
			@RequestParam(value="username") String username,
			@RequestParam(value="age", required=false, defaultValue="0") int age) {
		System.out.println("testRequestParam, username="+username+",age="+age);
		return "success";
	}
	
}

 

【说明】:

(1)如果int类型的age设置为required=false,则当没有传递该值时,系统默认会将null赋值给age,此时会报错,因为age是基本数据类型int,可以将age修改为Integer类型,或者设置age的defaultValue="0"。

(2)在接收参数时,如果不指明@RequestParam,则也会接收到值,只是此时少了非空或默认值的设置。

 

 3、访问代码

<a href="<%=path%>/springmvc/testRequestParam?username=lps&age=31">Test RequestParam</a>

 

 

分享到:
评论

相关推荐

    SpringMVC注解@RequestParam方法原理解析

    主要介绍了SpringMVC注解@RequestParam方法原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    Spring注解 - 52注解 - 原稿笔记

    注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...

    Controller注解学习笔记.md

    整理笔记:在springboot中的各个注解的作用,包含@Controller、@ResponseBody、@RestController、@RequestMapping、@GetMapping 注解、@SpringBootTest注解:、@RequestParam注解、@Param注解、@pathVariable注解

    springboot后台接收axios传递的json数据为null

    看这两个注解的作用 @RequestParam 是作用在形参列表上,RequestParam可以接受简单类型的属性,也可以接受对象类型。在前端传入的是json字符串,后台按字符串string参数接收再解析。 请求头contentType设置为...

    springboot 控制层参数校验插件

    * @Check包含@RequestParam原生注解的所有功能,唯一修改的是将name值当做返回提示中的字段名来使用。 * 当@RequestParam和@Check同时作用于方法参数上时,@Check不起作用,以@RequestParam为准。 详情: ...

    使用postman传递数组调试

    使用postman传递数组 以springboot两个接收参数的注解为例:@RequestBody和@RequestParam ...二、进入正题,使用postman来传递@RequestBody和@RequestParam注解的数组参数 用postman传递@RequestBody

    spring mvc注释文档

    本文内容包括: ... 通过 @RequestParam 注解指定 • 清单 11. 使模型对象的特定属性具有 Session 范围的作用域 • 请求处理方法的签名规约 • 注册自己的属性编辑器 • 如何准备数据 • 小结 • 参考资料

    使用Spring2.5基于注解驱动的SpringMVC

    通过@RequestParam注解指定清单11.使模型对象的特定属性具有Session范围的作用域请求处理方法的签名规约注册自己的属性编辑器如何准备数据小结参考资料基于注解的配置有越来越流行的趋势,Spring2.5顺应这种趋势,为...

    Spring MVC RequestParam.docx

    对应@RequestParam基本类型的参数我们最好都使用包装类型 还有相识的注解 @RequestHeader。使用方式和@RequestParam一样。

    Spring Boot最常用的30个注解.docx

    详细介绍了Spring Boot最常用的30个注解,包含概念、原理、示例 Spring Boot最常用的30个注解 一、 @SpringBootApplication 二、 Spring Bean 相关 1 @Controller 2 @Service 3 @Repository 4 @Component 5 @Bean 6 ...

    Java面试可能问的问题.docx

    @RequestParam @PathViriable @Component 在类定义之前添加@Component注解,他会被spring容器识别,并转为bean。 @Repository 对Dao实现类进行注解 (特殊的@Component) @Service 用于对业务逻辑层进行注解, (特殊...

    SpringMVC注解

    SpringMVC注解@RequestParam全面解析

    开源bbs源码java-2207springboot:2207弹簧靴

    @RequestParam注解也参与其中,@RequestParam标记的属性不同映射的关系就不同,所有如果前段name属性和后台@RequestParam标记的属性名不相同就会报404 2020/03/05 发单管理 layui 在弹出层等小窗口显示页面时 可以用...

    Java课程实验 Spring Boot 文件上传与下载(源代码+实验报告)

    控制器可以使用@PostMapping注解来处理POST请求,并使用@RequestParam("file")来接收上传的文件 3.创建文件上传的表单: 创建一个HTML表单用于上传文件。在表单中使用enctype="multipart/form-data"属性来指定文件...

    springMVC技术概述

    常用注解:@Controller @RestController(Controller+ResponseBody) @Service @Transactional @Mapper @AutoWired @RequestMapping--路由 @RequestParam--参数绑定(不同名参数或Map&lt;Object,String&gt;) @...

    springMVC详解以及注解说明

    注解介绍等详细说明及使用: • @Controller • @Service • @Autowired • @RequestMapping • @RequestParam • @ModelAttribute • @Cacheable • @CacheFlush • @Resource • @PostConstruct • @...

    Java学习资料+SpringMVC

    1、什么是MVC 2、什么是SpringMVC 3、SpringMVC的特点 4、创建请求控制器 5、创建springMVC的配置文件 配置web.xml @RequestMapping注解 @RequestParam @RequestHeader @CookieValue 通过POJO获取请求参数

    Spring MVC之@RequestMapping详解

    前段时间项目中用到了REST风格来开发程序,但是当用POST、PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加任何注解),查看了提交方式为application/json, 而且服务器端通过request....

    spring mvc RequestParam与 ModelAttribute异同.docx

    关于spring mvc中的两个注解:@RequestParam、@ModelAttribute区别,原先并没有特别注意,直到最近找别人开发的一个小模块的bug时,才有意识的比较了两者的区别。

Global site tag (gtag.js) - Google Analytics