Spring MVC @RequestParam

案例来说明

1 @RequestMapping("user/add")
2 public String add(@RequestParam("name") String name,
3             @RequestParam("age") int age){
4         System.out.println(name+","+age);
5     return "hello";
6 }

测试1

当我们请求路径为:http://localhost:8080/springmvc-1/user/add?name=caoyc&age=18

输出结果:caoyc,18

测试2

当我请求路径为:http://localhost:8080/springmvc-1/user/add?age=18

输出结果:有异常出现。意思是说必须要有该参数

解决方案:在@RequestParam标签中添加一个required=false,表示该属性不是必须的

1 @RequestParam(value="name",required=false)

输出结果:null,18

 测试3

当我请求路径为:http://localhost:8080/springmvc-1/user/add?name=caoyc

同样出现上面的异常

那么根据上面的方法设置

1 @RequestParam(value="age",required=false) int age

结果再运行。还是抛出异常

这里也说到很明白,大概意思是说不能讲一个null的空值赋给age。应该使用包装类型

那么我们将代码改成这样:

1 @RequestParam(value="age",required=false) Integer age

结果正确输出:caoyc,null

这里还有另外一种改法:给参数指定一个默认值

1 @RequestParam(value="age",required=false,defaultValue="0") int age

结果输出:caoyc,0

【总结】对应@RequestParam基本类型的参数我们最好都使用包装类型

还有相识的注解

@RequestHeader。使用方式和@RequestParam一样。这里就不做多的讲解了。

时间: 2024-08-28 17:07:30

Spring MVC @RequestParam的相关文章

spring mvc:@RequestParam与@ModelAttribute异同

关于spring mvc中的两个注解:@RequestParam.@ModelAttribute区别,原先并没有特别注意,直到最近找别人开发的一个小模块的bug时,才有意识的比较了两者的区别. 1.@RequestParam,@RequestParam("xx") 表示在前端传递过来的参数中必须有个参数名称为"xx"(可以使用require=false避免必须) 2.@ModelAttribute,@ModelAttribute("xx") 表示

四、Spring MVC的RequestParam注解

前面的章节,有提到可以通过@PathVariable注解来映射restful风格的url中的值到方法中去,本章就看看如果不使用restful风格的url来怎么进行参数的传递. RequestParam就是来实现参数传递的,能够把用户的输入绑定到后台的方法上面.它有三个主要的属性: value:定义参数的名称 required:定义参数是否是必须的,默认是true defaultValue:定义参数的默认值 下面结合具体的示例代码来看一下如何使用: 1.下面的这段代码定义了三个参数,一个name,

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestParam, @PathVariable--转载

原文地址: @RequestMapping is one of the most widely used Spring MVC annotation.org.springframework.web.bind.annotation.RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be appl

Spring MVC 后台获取前台form参数值(requestparam)

Spring MVC 获取前台参数值得java代码: @RequestMapping(value = "/loginCheck") public String loginCheck( @RequestParam("username") String username, @RequestParam("password") String password) { UserMess user = new UserMess(); user.setUsern

Spring MVC HTTP请求数据绑定

package com.springmvc.controller; import com.springmvc.model.UserInfo; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.stereotype.Controller; import org.springframework.uti

编写Spring MVC Controller

1.映射请求 在POJO类定义处标注@Controller,再通过<content:component-scan /...>扫描相应的类包,即可使POJO类成为一个能处理HTTP请求的控制器. 如何将请求映射到对应的控制器的方法中是Spring MVC框架最重要的任务之一,这项任务由@RequestMapping注释承担. 例子1: 1 @Controller 2 public class UserController{ 3 4 @RequestMapping(value="/use

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestPar

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestParam, @PathVariable Pankaj July 4, 2014 Spring @RequestMapping is one of the most widely used Spring MVC annotation.org.springframework.web.bind.annotat

Spring MVC中使用Swagger生成API文档和完整项目示例Demo,swagger-server-api

本文作者:小雷FansUnion-一个有创业和投资经验的资深程序员-全球最大中文IT社区CSDN知名博主-排名第119 实际项目中非常需要写文档,提高Java服务端和Web前端以及移动端的对接效率. 听说Swagger这个工具,还不错,就网上找了些资料,自己实践了下. 一:Swagger介绍 Swagger是当前最好用的Restful API文档生成的开源项目,通过swagger-spring项目 实现了与SpingMVC框架的无缝集成功能,方便生成spring restful风格的接口文档,

Spring MVC的常用注解

1.@Controller 控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Model 返回给对应的View 进行展示. @COntroller用于标记一个类,即控制类,spring使用扫描机制查找应用程序中所有基于注解的控制类.分发处理器会扫描使用了该注解的类的方法,并检测该方法是否使用了@RequestMapping注解,而使用@RequestMapping注解的方法才是真正处理请