SpringMVC @RequestParam
案例来说明
@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的更多相关文章
- SpringMvc @RequestParam 使用推荐使用包装类型代替包装类型
SpringMvc 中@RequestParam注解使用 建议使用包装类型来代替基本数据类型 public String form2(@RequestParam(name="age" ...
- springMVC --@RequestParam注解(后台控制器获取參数)
在SpringMVC后台控制层获取參数的方式主要有两种,一种是request.getParameter("name"),第二种是用注解@RequestParam直接获取. 1.获取 ...
- 【转】springmvc @RequestParam
在SpringMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取.这里主要 ...
- SpringMvc@RequestParam 来映射请求参数
jsp页面 <a href="springmvc/testRequestParam?username=atguigu&age=11">Test RequestP ...
- springMVC --@RequestParam注解(后台控制器获取参数)
在SpringMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取. 1.获 ...
- springmvc RequestParam、RequestHeader
/** * 了解: * * @CookieValue: 映射一个 Cookie 值. 属性同 @RequestParam */ @RequestMapping("/testCookieVal ...
- springmvc @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...
- SpringMVC @RequestParam和@RequestBody的区别
问题:@Requestbody 用的时候遇到400和415错误,因为请求格式不对. @RequestBody @RequestBody能把简单json结构参数转换成实体类,如下代码: @Request ...
- SpringMVC框架的基础知识;
首先 在javaEE环境下,建立一个动态的web工程: 导入架包.... 建立一对多映射关系的封装类,这儿只写属性,getter和setter方法就不写了: 1: private String pro ...
随机推荐
- nodejs之流数据读取与写入
1.(fs.createReadStream)当文件比较大时,建议使用文件流读取,不会出现卡顿现象,demo如下. const fs = require('fs'); //流的方式读取文件 var r ...
- nodejs之简单应用与运行
1.nodejs第一个应用,入口函数为http.createServer() var http=require('http');//1.引入 http 模块 //2.用 http 模块创建服务 htt ...
- Prism学习--实现可插拔的模块
首先,在使用Prism框架加载的程序集中分别添加一个类,并让这些类实现IModule接口.当Prism框架加载某个程序集后,将首先在程序集中搜索实现了该接口的类.之后将会调用该接口的Initializ ...
- Oracle 数据字典视图(V$,GV$,X$)
常用的几个数据字典: user_objects : 记录了用户的所有对象,包含表.索引.过程.视图等信息,以及创建时间,状态是否有效等信息,是非DBA用户的大本营.想知道自己有哪些对象,往这里查. u ...
- 安装kafka 记录
sudo ./zoo /zoo /bin/zkServer.sh start sudo ./kafkacluster/kafka1/bin/kafka-server-start.sh ./kafkac ...
- OpenCV 中获取图像或矩阵最大、最小值的简便方法
C++: void minMaxLoc(InputArray src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc ...
- normalization(统计)
In statistics and applications of statistics, normalization can have a range of meanings.[1] In the ...
- python学习之socket&黏包
7.4 socket [重要] 避免学习各层的接口,以及协议的使用, socket已经封装好了所有的接口,直接使用这些接口或者方法即可,方便快捷,提升开发效率. socket在python中就是一 ...
- 关于eclipse设置JRebel
版本:eclipse ee Version: 2018-09 (4.9.0) jrebel:最新2019-2 1.在eclipse->help->eclipse Marketplace 2 ...
- [LeetCode] 226. 用队列实现栈
题目链接: https://leetcode-cn.com/problems/implement-stack-using-queues 难度:简单 通过率:59.9% 题目描述: 使用队列实现栈的下列 ...