【1】params

params: 指定request中必须包含某些参数值是,才让该方法处理。

    @RequestMapping(value = "testParamsAndHeaders", params = { "username","age!=10" })
public String testParamsAndHeaders() {
System.out.println("testParamsAndHeaders");
return SUCCESS;
}
  • 1
  • 2
  • 3
  • 4
  • 5

params 只是判断url 或者 form data 中的参数是否复合params的定义,并不会直接绑定数据到方法的参数中!


【2】@PathVariabl

  • 绑定路径中的占位符参数到方法参数变量中;

  • 只能绑定路径中的占位符参数,且路径中必须有参数。

无论是 GET 或者POST 只要 URL中有参数即可!

如:

  • GET
Request URL:http://localhost:8080/SpringMVC-1/springmvc/testPathVariable/1
  • 1
  • POST
    <form action="springmvc/testPathVariable/1" method="POST">
<input type="text" name="username" value=""/>
<input type="text" name="age" value=""/>
<input type="text" name="sex" value=""/>
<input type="submit" value="submit"/>
</form>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

【注意:】如果URL中无参数,将会出错;如果URL有参数,但是没有使用@PathVariabl该注解,那么URL的参数不会默认与方法参数绑定!方法里的参数会默认绑定表单里面对应的参数!


  • 后台code

如果参数名与占位符一致,则可直接使用@PathVariable;

如果不一致,则在@PathVariable( )括号内绑定占位符。

    @RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") Integer id2) {
System.out.println("testPathVariable: " + id2);
return SUCCESS;
}
  • 1
  • 2
  • 3
  • 4
  • 5

【3】@RequestParam

  • value:参数key,可以不写,默认为”“;

  • name:和value作用一样;

  • required:默认值为true,可以不写;

  • 获取URL或者 form data 中的参数


  • GET
<a href="springmvc/testRequestParam?userName=tom&age=11&sex=boy">
  • 1
  • POST
    <form action="springmvc/testRequestParam" method="POST">
<input type="text" name="userName" value=""/>
<input type="text" name="age" value=""/>
<input type="text" name="sex" value=""/>
<input type="submit" value="submit"/>
</form>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

注意 :

GET中的参数形式为:username=tom&age=11&sex=boy

POST中的参数形式为:以键值对形式保存在form data


后台:

    @RequestMapping(value="/regist",produces="application/json;charset=utf-8")
@ResponseBody
public String regist(SysUser sysUser , @RequestParam(required=true,name="sex") String sex){
String userName = sysUser.getUserName();
String age = sysUser.getAge();
//...
return "regist success";
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

总得来说,均是键值对形式。与@PathVariabl中的占位符形式不同!!!

原文链接   :  https://blog.csdn.net/j080624/article/details/56280382

@params、@PathVariabl和@RequestParam用法与区别的更多相关文章

  1. [转载]jQuery中wrap、wrapAll和wrapInner用法以及区别

    原文地址:jQuery中wrap.wrapAll和wrapInner用法以及区别作者:伊少君 原文: <ul>   <li title='苹果'>苹果</li>   ...

  2. WordPress翻译中 __()、_e()、_x、_ex 和 _n 的用法及区别

    编译函数 WordPress使用了下面几个函数来方便语言本地化. __() _e() _x() _ex() _n() 以上所列的函数是用来包含所需翻译的字符串的,根据字符串的不同参数和输出类型,需要使 ...

  3. typedef和#define的用法与区别

    typedef和#define的用法与区别 typedef和#define的用法与区别 一.typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译过程 ...

  4. SQL语句中count(1)count(*)count(字段)用法的区别

    SQL语句中count(1)count(*)count(字段)用法的区别 在SQL语句中count函数是最常用的函数之一,count函数是用来统计表中记录数的一个函数, 一. count(1)和cou ...

  5. Collection List Set和Map用法与区别

    labels:Collection List Set和Map用法与区别 java 散列表 集合 Collection           接 口的接口      对 象的集合   ├   List   ...

  6. jQuery 中 children() 与 find() 用法的区别

    1.children() 与 find() 用法的区别 通过children获取的是该元素的下级元素,而通过find获取的是该元素的下级所有元素.

  7. Linux中yum和apt-get用法及区别

    Linux中yum和apt-get用法及区别   一般来说著名的linux系统基本上分两大类:   1.RedHat系列:Redhat.Centos.Fedora等   2.Debian系列:Debi ...

  8. Java中“|”和“||”用法的区别

    例子: int a = 5; int b = 10; if(a > 4 | b++ > 10) { System.out.println("a:"+a+"\n ...

  9. Html A标签中 href 和 onclick用法、区别、优先级别

    原文:Html A标签中 href 和 onclick用法.区别.优先级别 如果不设置 href属性在IE6下面会不响应hover.双击后会选中标签的父容器而非这个一a标签(IE下都存在这一问题). ...

随机推荐

  1. 学习笔记2:postman 的基本使用

    1.get 请求 请求接口:http://xx.xx.xx.xx/api/user/stu_info 请求参数:stu_name 请求参数是放在请求 URL 里的,点击 Params添加参数,key ...

  2. 在系统中使用Bean Validation验证参数

    转自:http://www.importnew.com/18561.html 为什么要使用Bean Validation?  当我们实现某个接口时,都需要对入参数进行校验.例如下面的代码 1 2 3 ...

  3. func 的参数修饰

    1.0 在声明一个 Swift的方法的时候,我们一般不去指定参数前面的修饰符,而是直接声明参数: func incrementor(variable : Int) ->Int { } 这个方法接 ...

  4. 吴裕雄 python 爬虫(4)

    import requests user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, li ...

  5. Mac中opencv批量对图片进行二值化

    对灰度图像进行二值化,传入的图片是手写汉字的截图,通过二值化把字的部分提出来.用ostu进行二值化 #include <stdio.h> #include <iostream> ...

  6. Failed to read schema document 'http://www.springframework.org/schema/beans/spring-beans.xsd'

    明明项目没错误,但application.xml就报了错误,这是什么问题呢? 问题在于我们找不到org/springframework/beans/spring-beans这个包,也就是我们的spri ...

  7. SQL Server和MySQL数据库

    导读:接下来的网上商城的项目,需要用到MySQL数据库了.这个对于我来说,是一个新接触的东西,按照惯例,在刚开始学习一个东西的时候,先从宏观上去了解它.本篇博客,先介绍SQL Server的基本内容, ...

  8. Compute Shader

    [Compute Shader] 1.Similar to regular shaders, compute shaders are Asset files in your project, with ...

  9. django admin后台设置

    #encoding:utf-8 from django.contrib import admin from son10.models import * # Register your models h ...

  10. JMeter学习(三十七)Jmeter录制手机app脚本(转载)

    转载自 http://www.cnblogs.com/yangxia-test 环境准备: 1.手机 2.wifi 3.Jmeter   具体步骤: 1.启动Jmeter: 2.“测试计划”中添加“线 ...