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

一、基本使用,获取提交的参数

后端代码:

  1. @RequestMapping("testRequestParam")
  2. public String filesUpload(@RequestParam String inputStr, HttpServletRequest request) {
  3. System.out.println(inputStr);
  4. int inputInt = Integer.valueOf(request.getParameter("inputInt"));
  5. System.out.println(inputInt);
  6. // ......省略
  7. return "index";
  8. }

前端代码:

  1. <form action="/gadget/testRequestParam" method="post">
  2. 参数inputStr:<input type="text" name="inputStr">
  3. 参数intputInt:<input type="text" name="inputInt">
  4. </form>

前端界面:

执行结果:

test1

123

可以看到spring会自动根据参数名字封装进入,我们可以直接拿这个参数名来用

二、各种异常情况处理

1、可以对传入参数指定参数名

  1. @RequestParam String inputStr
  2. // 下面的对传入参数指定为aa,如果前端不传aa参数名,会报错
  3. @RequestParam(value="aa") String inputStr

错误信息:

HTTP Status 400 - Required String parameter 'aa' is not present

2、可以通过required=false或者true来要求@RequestParam配置的前端参数是否一定要传

  1. // required=false表示不传的话,会给参数赋值为null,required=true就是必须要有
  2. @RequestMapping("testRequestParam")
  3. public String filesUpload(@RequestParam(value="aa", required=true) String inputStr, HttpServletRequest request)

3、如果用@RequestMapping注解的参数是int基本类型,但是required=false,这时如果不传参数值会报错,因为不传值,会赋值为null给int,这个不可以

  1. @RequestMapping("testRequestParam")
  2. public String filesUpload(@RequestParam(value="aa", required=true) String inputStr,
  3. @RequestParam(value="inputInt", required=false) int inputInt
  4. ,HttpServletRequest request) {
  5. // ......省略
  6. return "index";
  7. }

解决方法:

    “Consider declaring it as object wrapper for the corresponding primitive type.”建议使用包装类型代替基本类型,如使用“Integer”代替“int”

SpringMVC注解@RequestParam全面解析的更多相关文章

  1. SpringMVC注解@RequestParam全面解析---打酱油的日子

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

  2. SpringMVC注解@RequestParam与RequestMapping全面解析

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

  3. SpringMVC注解@RequestParam(转)

    鸣谢:http://shawnccx.iteye.com/blog/730239 -------------------------------------------------- 在SpringM ...

  4. SpringMVC注解@RequestParam解析

    1.可以对传入参数指定参数名 1 @RequestParam String inputStr 2 // 下面的对传入参数指定为param,如果前端不传param参数名,会报错 3 @RequestPa ...

  5. springmvc 注解 RequestParam/RequestHeader/CookieValue

    RequestParam注解: 示例: @RequestMapping("/testRequestParam") public String testRequestParam(@R ...

  6. SpringMVC注解@RequestMapping全面解析---打酱油的日子

    @RequestMapping 可以出现在类级别上,也可以出现在方法上.如果出现在类级别上,那请求的 url 为 类级别上的@RequestMapping + 方法级别上的 @RequestMappi ...

  7. SpringMVC注解@RequestMapping全面解析

    @RequestMapping 可以出现在类级别上,也可以出现在方法上.如果出现在类级别上,那请求的 url 为 类级别上的@RequestMapping + 方法级别上的 @RequestMappi ...

  8. SpringMVC注解@RequestMapping @RequestParam @ResponseBody 和 @RequestBody 解析

    SpringMVC Controller层获取参数及返回数据的方式: @RequestMapping @RequestMapping(“url”),这里的 url写的是请求路径的一部分,一般作用在 C ...

  9. springMVC的注解@RequestParam与@PathVariable的区别

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

随机推荐

  1. image hover

    http://www.nxworld.net/tips/css-image-hover-effects.html

  2. jwplayer直播

    <div class='container'> <div class='row'> <div class='col-sm-10 col-md-10 col-sm-offs ...

  3. 新建android项目报错,代码中找不到错误

    通过网上资料的引导,做以下操作: 1.进入C:\Documents and Settings\Administrator\.android 删除路径下的debug.keystore及 ddms.cfg ...

  4. pro9

    1.本次课学习到的知识点 C语言的几个基本数据类型 各种基本数据类型的常量的表现形式 C语言的表达式个中表达式的求解规则 2.实验过程中遇到的问题及解决方法: 不太理解完数的概念以及如何判断完数,另外 ...

  5. Win7下VS2008编译QtiPlot

    Win7下VS2008编译QtiPlot By wangsh 2011-11-25 QtiPlot 是一款开源免费的科学绘图软件,可运行在多个平台(windows.Linux等)中,从功能上讲,Qti ...

  6. 让Dreamweaver支持less

    编辑->首选参数->文件类型/编辑器->在代码视图中打开->添加" .less"后缀

  7. android判断pad还是手机

    第一种. Rect frame = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); if (fr ...

  8. iOS NSURLSession 封装下载类

    周六日鼓捣NSURLSession,效率虽然低下,最后还是有了一点点眉目.昨天和汤老师一起测试,又对它加深了一点理解.趁热打铁,先总结一下. 封装的类,对外用的方法,我写的是类方法,所以,在类方法中, ...

  9. C#遍历List并删除某个或者几个元素的方法

    C#遍历List并删除某个或者几个元素的方法,你的第一反应使用什么方法实现呢?foreach? for? 如果是foreach,那么恭喜你,你答错了.如果你想到的是用for,那么你只是离成功进了一步. ...

  10. WPF数据库连接错误:The user is not associated with a trusted SQL Server connection.

    我当初安装sql server的时候选的Window Authentication mode,没选SQL Server Windows Authentication. 后来做WPF时连接数据库时需要一 ...