在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. MYSQL PASSWORD()

    https://www.pythian.com/blog/hashing-algorithm-in-mysql-password-2/ SELECT PASSWORD ("this_is_a ...

  2. 三种查看SqlServer中数据物理pge页的方法

    1.根据数据记录查看当前记录所在的文件编号.page页.以及在页中的插槽. 示例如下: SELECT top %%physloc%%, sys.fn_physlocFormatter (%%physl ...

  3. php curl多线程抓取网页

    PHP 利用 Curl Functions 可以完成各种传送文件操作,比如模拟浏览器发送GET,POST请求等等,受限于php语言本身不支持多线程,所以开发爬虫程序效率并不高,这时候往往需 要借助Cu ...

  4. SQLSERVER 表名数据库名作为变量 必须使用动态SQL(源自网络)

    动态语句基本语法: 1 :普通SQL语句可以用exec执行 Select * from tableName exec('select * from tableName') exec sp_execut ...

  5. Swift-04-Designated&&Convenience

    class ClassA { let numA:Int init(num: Int){ numA = num } } class ClassB: ClassA { let numB:Int overr ...

  6. Selenium2学习-013-WebUI自动化实战实例-011-WebElement.getText()值为空问题探索及解决

    今天有个朋友在群里问 WebElement.getText() 值为空,当你发现取到的值为空的时候,会不会郁闷呢?明明看到的值不为空,脚本看着也没有问题啊,为何取到的值为空呢!!!万千纠结啊,若是长时 ...

  7. Java学习-013-文本文件读取实例源代码(两种数据返回格式)

    此文源码主要为应用 Java 读取文本文件内容实例的源代码.若有不足之处,敬请大神指正,不胜感激! 1.读取的文本文件内容以一维数组[LinkedList<String>]的形式返回,源代 ...

  8. zero3- JPA http://www.cnblogs.com/holbrook/archive/2012/12/30/2839842.html

    1.很好的博客:http://www.cnblogs.com/holbrook/archive/2012/12/30/2839842.html 2. 最新的搬到github : http://holb ...

  9. 另一个SqlParameterCollection中已包含SqlParameter

    一般情况下,我们定义的一个SqlParameter参数数组,如: SqlParameter[] parms =             {                new SqlParamete ...

  10. POJ 1028题目描述

    Description Standard web browsers contain features to move backward and forward among the pages rece ...