鸣谢:http://shawnccx.iteye.com/blog/730239

--------------------------------------------------

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

一、基本使用,获取提交的参数 
后端代码:

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

前端代码:

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

前端界面:

执行结果: 
test1 
123

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

二、各种异常情况处理 

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

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

错误信息: 
HTTP Status 400 - Required String parameter 'aa' is not present

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

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

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

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

解决方法: 
    “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全面解析

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

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

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

  4. springmvc 注解 RequestParam/RequestHeader/CookieValue

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

  5. SpringMVC注解@RequestParam解析

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

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

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

  7. springMVC中@RequestParam和@RequestBody注解的用法

    springMVC中@RequestParam注解用在Controller层获解析.提取参数,当然你也可以用request.getParameter("name")来获取参数,而@ ...

  8. springMVC中的注解@RequestParam与@PathVariable的区别

    1.@PathVariable @PathVariable绑定URI模板变量值 @PathVariable是用来获得请求url中的动态参数的 @PathVariable用于将请求URL中的模板变量映射 ...

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

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

随机推荐

  1. MongoDB - Introduction to MongoDB, Documents

    MongoDB stores data records as BSON documents. BSON is a binary representation of JSON documents, th ...

  2. ASP常用函数表

    ASP常用函数表(新手们的好工具) 作者:未知 Array() 函数返回一个数组 表达式 Array(list) 允许数据类型: 字符,数字均可 实例: <% Dim myArray() For ...

  3. 转 Android 4.0后,自定义Title报错 You cannot combine custom titles with other title feature

      自定义Titlebar时为了避免冲突 需要修改:AndroidManifest.xml android:theme="@style/mystyle" styles.xml文件中 ...

  4. Android之图片应用

    package com.example.imagescale; import android.os.Bundle; import android.app.Activity; import androi ...

  5. 那万恶的ssh真是麻烦

    设置为允许某组远程ssh ,key也放入相应的服务器了,死活提示,Permission denied (publickey). 而偏偏另外的用户相同的配置却又可以,这里找到答案,原来是跟目录的权限有关 ...

  6. 你早该这么玩Excel 读书笔记

    1. Excel用来分析数据,至少要有一份原始数据和对于的分类汇总数据,这两种数据在一项任务中,应该是存放在同一个Excel文档中的,在书籍中,把他们叫做源数据表和分类汇总表.用户输入源数据表,根据相 ...

  7. oracle 中proc和oci操作对缓存不同处理

    oracle 中proc和oci操作对缓存不同处理

  8. Poj 2996 Help Me with the Game

    1.Link: http://poj.org/problem?id=2996 2.Content: Help Me with the Game Time Limit: 1000MS   Memory ...

  9. Poj OpenJudge 1068 Parencodings

    1.Link: http://poj.org/problem?id=1068 http://bailian.openjudge.cn/practice/1068 2.Content: Parencod ...

  10. 怎样在Android SDK 下查看应用程序输出日志的方法

          该文章源于安卓教程网(http://android.662p.com),转载时要注明文章的来自和地址,感谢你的支持. 在Android程序中可以使用 android.util.Log 类来 ...