案例来说明

  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一样。这里就不做多的讲解了。

Spring MVC @RequestParam的更多相关文章

  1. spring mvc:@RequestParam与@ModelAttribute异同

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

  2. spring mvc@RequestParam根据参数名获取传入参数值

    在SpringMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取.这里主要 ...

  3. Spring MVC @RequestParam @RequestHeader @CookieValue用法

    摘要: package com.hust.springmvc1; import org.springframework.stereotype.Controller; import org.spring ...

  4. Spring MVC @RequestParam(5)

    案例来说明 1 @RequestMapping("user/add") 2 public String add(@RequestParam("name") St ...

  5. spring mvc注解@RequestParam

    在spring mvc 的使用过程中 获取 页面传来的参数的时候,我平时都习惯  @RequestParam String name,突然有一天我发现 直接在方法参数后面写 String name , ...

  6. 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.b ...

  7. Spring MVC 注解 @RequestParam解析

    在Spring MVC 后台控制层获取参数的方式主要有两种,一种是requset.getParameter(“name”),另一种是用注解@Resquest.Param直接获取. 一.基本使用获取提交 ...

  8. spring mvc:练习 @RequestParam(参数绑定到控制器)和@PathVariable(参数绑定到url模板变量)

    spring mvc:练习 @RequestParam和@PathVariable @RequestParam: 注解将请求参数绑定到你的控制器方法参数 @PathVariable: 注释将一个方法参 ...

  9. 【spring mvc】spring mvc POST方式接收单个字符串参数,不加注解,接收到的值为null,加上@RequestBody,接收到{"uid":"品牌分类大”},加上@RequestParam报错 ---- GET方式接收单个参数的方法

    spring mvc POST方式 接收单个参数,不加任何注解,参数名对应,接收到的值为null spring mvc POST方式 接收单个参数,加上@RequestBody,接收到参数格式:{&q ...

随机推荐

  1. Java线程Run和Start的区别

    先上结论:run只是Thread里面的一个普通方法,start是启动线程的方法.何以见得呢?可以执行下面的代码看看run和start的区别: package com.basic.thread; /** ...

  2. nginx main函数

    源代码: int ngx_cdecl main(int argc, char *const *argv) { ngx_int_t i; ngx_log_t *log; ngx_cycle_t *cyc ...

  3. react native组件的创建

    react native组件的创建 react文件加载顺序: react项目启动后,先加载index.js.在index.js中可以指向首页. import { AppRegistry } from ...

  4. Cube Stacking P0J 1988(加权并查集)

    Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes ...

  5. 深度系统(deepin)与win10双系统切换设置

    之前在win10下安装了深度系统,我不知道其他人在双系统切换的时候是否需要更改BIOS参数,我根据我的实际情况给出双系统切换设置的解决方案. 1.开机后进入选项System setup 2.按照下图选 ...

  6. 20181120-10 Beta阶段第2周/共2周 Scrum立会报告+燃尽图 7

    此作业要求参见:[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2415] 版本控制地址    [https://git.coding.n ...

  7. 20162328蔡文琛 Bag类

    在刚刚开始着手这个作业时,想的是使用for循环来自己写出add等方法来,但是在看过API后知道了Arraylist这个java已有的列表类,于是就只用ArrayList的方法很快的就做了出来.在进行B ...

  8. Unicode和UTF-8

    作者:于洋链接:https://www.zhihu.com/question/23374078/answer/69732605来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  9. KMP的原理和代码实现(详细注释|参考多个博客总结|可作为模板)

    KMP算法解决的问题是字符匹配,是由Knuth–Morris–Pratt共同开发出来的,这个算法把字符匹配的时间复杂度缩小到O(m+n),而空间复杂度也只有O(m),n是target的长度,m是pat ...

  10. HDU 5496 Beauty of Sequence

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5496 Beauty of Sequence Problem Description Sequence ...