鸣谢: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. Springmvc+uploadify实现文件带进度条批量上传

    网上看了很多关于文件上传的帖子,众口不一,感觉有点乱,最近正好公司的项目里用到JQuery的uploadify控件做文件上传,所以整理下头绪,搞篇文档出来,供亲们分享. Uploadify控件的主要优 ...

  2. python学习day1--python基础

    Python的优缺点 先看优点 Python的定位是“优雅”.“明确”.“简单”,所以Python程序看上去总是简单易懂,初学者学Python,不但入门容易,而且将来深入下去,可以编写那些非常非常复杂 ...

  3. asp.net中C#对象与方法 属性详解

    C#对象与方法 一.相关概念: 1.对象:现实世界中的实体 2. 类:具有相似属性和方法的对象的集合 3.面向对象程序设计的特点:封装  继承 多态 二.类的定义与语法 1.定义类: 修饰符 类名称 ...

  4. 页面所有的button绑定同一个事件,点击不同的button赋值不同

    <script type="text/javascript"> $(function(){ $("input[type='button']").cl ...

  5. iOS开发——app审核指导方针(官网)

    iOS 开发后上传到App Store审核的指导方针 ——苹果官网介绍地址 https://developer.apple.com/app-store/review/guidelines/

  6. 找不到System.Runtime.Serialization.Json的解决方案

    System.ServiceModel System.ServiceModel.Web System.Runtime.Serialization 三者均要添加引用

  7. c#基础学习笔记-----------委托事件

    这里有一个比较简单的委托实例应用(跨窗体操作控件) http://blog.csdn.net/bdstjk/article/details/7004035 还有一个比较详细的介绍并深入理解委托事件的讲 ...

  8. Contest1065 - 第四届“图灵杯”NEUQ-ACM程序设计竞赛(个人赛)D拿糖果

    题目描述 薯片和他的朋友薯条来到了商店,商店有n个糖果,标号依次为1,2,3....n,对应的价值为W1,W2,W3...Wn.现在薯片先拿走一个标号为a的糖果,标号小于a的糖果就被商家收回去了,然后 ...

  9. template_11实参演绎

    1,演绎过程匹配类型A(来自实参的类型),参数化类型P(行参参数声明)如果被声明的参数是一个引用声明g(T& )那么P就是所引用类型T:f(T)中P就是所声明的参数类: decay指从数组和函 ...

  10. CCM加密学习

    这几天终于搞定了AES硬件加密工具的使用,几种简单的加密模式也都实验通过了,比较麻烦的一种是CCM模式的加密,它是CTR加密模式和CMAC认证算法的混合使用.本文先介绍CCM模式的原理与基本实现,然后 ...