案例来说明

@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. pureftp安装部署

    ftp:文件传输pure-ftpd:ftp软件作用:通过映射登录传送和接收文件.1.先下载pure-ftpd软件,最好指定一个下载软件目录如:/usr/local/src wget https://d ...

  2. 阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用

    resources下新建File文件 bean.xml 配置jdbcTemplate 注入DataSource 新建测试方法 复制demo1改个名字叫做demo2 编写Insert的方法

  3. apache虚拟目录配置实例

    apache虚拟目录配置实例 一.首先,开启虚拟主机配置 在文件httpd.conf中找到: include conf/extra/httpd-vhosts.conf #开启 二.对httpd-vho ...

  4. Python学习之==>数组(一)

    1.定义数组 city = [] # 定义一个空数组 name = ['Jack','Panda','Bob','Franck'] # 根据下标找元素,最前面一个元素的下标是0,最后一个元素下标是-1 ...

  5. 【SVN】更新提交失败---- Previous operation has not finished; run 'cleanup' if it was interrupted解决方法

     Previous operation has not finished; run 'cleanup' if it was interrupted 问题出处 解决方法 2017-11-01   08: ...

  6. Golang的面向对象编程【结构体、方法、继承、接口】

    Golang也支持面向对象编程.但与以前学过传统的面向对象编程语言有区别.1)Golang没有类class,Go语言的结构体struct和类class有相似的特性.2)Golang中不存在继承,方法重 ...

  7. cocos2dx基础篇(7) 触碰事件

    cocos2dx游戏引擎的重点是在于移动设备的跨平台开发,而移动设备上的游戏大部分都是通过屏幕触碰来进行的.比如主菜单的按钮触碰,打飞机中飞机的触碰移动,都需要用到触碰操作.想一想之前讲的菜单按钮CC ...

  8. 【转帖】国产x86处理器KX-6000发布

    国产最先进x86处理器KX-6000发布:8核3.0GHz 力压酷睿i5处理器 https://www.cnbeta.com/articles/tech/858981.htm 全网所有的网页都写错了 ...

  9. olap和Oltp(转)

    OLAP和OLTP的区别(基础知识) 联机分析处理 (OLAP) 的概念最早是由关系数据库之父E.F.Codd于1993年提出的,他同时提出了关于OLAP的12条准则.OLAP的提出引起了很大的反响, ...

  10. Zookeeper 和Eureka比较

    作为服务注册中心,Eureka比Zookeeper好在哪里著名的CAP理论指出,一个分布式系统不可能同时满足C(一致性).A(可用性)和P(分区容错性).由于分区容错性P在是分布式系统中必须要保证的, ...