案例来说明

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

测试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,表示该属性不是必须的

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

输出结果:null,18

 测试3

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

同样出现上面的异常

那么根据上面的方法设置

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

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

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

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

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

结果正确输出:caoyc,null

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

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

结果输出:caoyc,0

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

还有相识的注解

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

本文转自:http://www.cnblogs.com/caoyc/p/5635427.html

SpringMVC @RequestParam的更多相关文章

  1. SpringMvc @RequestParam 使用推荐使用包装类型代替包装类型

    SpringMvc 中@RequestParam注解使用 建议使用包装类型来代替基本数据类型 public String form2(@RequestParam(name="age" ...

  2. springMVC --@RequestParam注解(后台控制器获取參数)

    在SpringMVC后台控制层获取參数的方式主要有两种,一种是request.getParameter("name"),第二种是用注解@RequestParam直接获取. 1.获取 ...

  3. 【转】springmvc @RequestParam

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

  4. SpringMvc@RequestParam 来映射请求参数

    jsp页面 <a href="springmvc/testRequestParam?username=atguigu&age=11">Test RequestP ...

  5. springMVC --@RequestParam注解(后台控制器获取参数)

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

  6. springmvc RequestParam、RequestHeader

    /** * 了解: * * @CookieValue: 映射一个 Cookie 值. 属性同 @RequestParam */ @RequestMapping("/testCookieVal ...

  7. springmvc @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...

  8. SpringMVC @RequestParam和@RequestBody的区别

    问题:@Requestbody 用的时候遇到400和415错误,因为请求格式不对. @RequestBody @RequestBody能把简单json结构参数转换成实体类,如下代码: @Request ...

  9. SpringMVC框架的基础知识;

    首先 在javaEE环境下,建立一个动态的web工程: 导入架包.... 建立一对多映射关系的封装类,这儿只写属性,getter和setter方法就不写了: 1: private String pro ...

随机推荐

  1. MVC1:.Net MVC Cotroller向View传值

    下面介绍 ASP .Net MVC中 Cotroller 向 View 传值 的4中方式: ViewBag,ViewData,TempData,Model. (注:参数可根据需要为复杂类型,只需在应用 ...

  2. Idea创建项目后,提交项目到码云的问题

    1.创建git仓库:vcs->import int version control->Create git repository 2.在项目头点击右键->git->add 3. ...

  3. caffe-----使用C++ 提取网络中间层特征数据

    最近实验,想要在c++下知道网络中间某一层的特征数据情况,查找了相关资料,记录一下. 其实在caffe框架里面是包含这种操作的,可以模仿tools/extract_features.cpp中的操作来得 ...

  4. JMeter强大的性能测试工具

    JMeter强大的性能测试工具,可模拟服务器负载,进行性能测试 配合badboy采集请求数据.

  5. 异常1(Exception)

    父类 :Throwable(可抛出的) 有两个子类:Error(错误)       Exception(异常) Error是所有错误类的父类,Exception是所有异常类的父类. 如图所示: 格式: ...

  6. xshell登陆后脚本

    vbs的写法: Sub Main xsh.Screen.Send "ssh 用户名@服务器地址" xsh.Screen.Send VbCr xsh.Screen.WaitForSt ...

  7. [转帖]PostgreSQL pg_dump&psql 数据的备份与恢复

    PostgreSQL pg_dump&psql 数据的备份与恢复   https://www.cnblogs.com/chjbbs/p/6480687.html 文章写的挺好 今天试了下 挺不 ...

  8. Forsaken喜欢数论

    链接:https://ac.nowcoder.com/acm/contest/1221/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言1048 ...

  9. 读取CSV文件存入map中(C++)

    自己平时操作文件用的不多,今天小伙伴让帮忙写一下这个,顺便记一下.实现功能:从"翻译.csv"文件中读取出字符串,以","作为分隔符,将每一行对应存入map中. ...

  10. .net 分布式锁

    原文 : 浅解.Net分布式锁的实现   序言 我晚上有在公司多呆会儿的习惯,所以很多晚上我都是最后一个离开公司的.当然也有一些同事,跟我一样喜欢在公司多搞会儿.这篇文章就要从,去年年末一个多搞会的晚 ...