@params、@PathVariabl和@RequestParam用法与区别
【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用法与区别的更多相关文章
- [转载]jQuery中wrap、wrapAll和wrapInner用法以及区别
原文地址:jQuery中wrap.wrapAll和wrapInner用法以及区别作者:伊少君 原文: <ul> <li title='苹果'>苹果</li> ...
- WordPress翻译中 __()、_e()、_x、_ex 和 _n 的用法及区别
编译函数 WordPress使用了下面几个函数来方便语言本地化. __() _e() _x() _ex() _n() 以上所列的函数是用来包含所需翻译的字符串的,根据字符串的不同参数和输出类型,需要使 ...
- typedef和#define的用法与区别
typedef和#define的用法与区别 typedef和#define的用法与区别 一.typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译过程 ...
- SQL语句中count(1)count(*)count(字段)用法的区别
SQL语句中count(1)count(*)count(字段)用法的区别 在SQL语句中count函数是最常用的函数之一,count函数是用来统计表中记录数的一个函数, 一. count(1)和cou ...
- Collection List Set和Map用法与区别
labels:Collection List Set和Map用法与区别 java 散列表 集合 Collection 接 口的接口 对 象的集合 ├ List ...
- jQuery 中 children() 与 find() 用法的区别
1.children() 与 find() 用法的区别 通过children获取的是该元素的下级元素,而通过find获取的是该元素的下级所有元素.
- Linux中yum和apt-get用法及区别
Linux中yum和apt-get用法及区别 一般来说著名的linux系统基本上分两大类: 1.RedHat系列:Redhat.Centos.Fedora等 2.Debian系列:Debi ...
- Java中“|”和“||”用法的区别
例子: int a = 5; int b = 10; if(a > 4 | b++ > 10) { System.out.println("a:"+a+"\n ...
- Html A标签中 href 和 onclick用法、区别、优先级别
原文:Html A标签中 href 和 onclick用法.区别.优先级别 如果不设置 href属性在IE6下面会不响应hover.双击后会选中标签的父容器而非这个一a标签(IE下都存在这一问题). ...
随机推荐
- C++ 连接Oracle
下面是一个ADO方式连接Oracle的小程序部分代码...... 首先是Oracle的配置.在Oracle的安装路径下找到:Oracle\network\ADMIN\tnsnames.ora文件.配置 ...
- MVC之Model元数据
Contronoller激活之后,ASP.NET MVC会根据当前请求上下文得到目标Action的名称,然后解析出对应的方法并执行之. 在整个Action方法的执行过程中,Model元数据的解析是一个 ...
- Java基本语法知识要点
0x00 一个源文件中有多少个类,在用javac编译后,在同一目录下将产生多少个对应的字节码文件(.class ).类里面不一定要有public static void main(String[] ...
- Class-reference types 类引用类型--快要失传的技术
先摘一段原版的说明: A class-reference type, sometimes called a metaclass, is denoted by a construction of the ...
- SQL Server Assembly (SQL CLR) 还原数据库后的问题
最近弄项目迁移的时候遇到还原数据库(SQL Server 2008)后遇到的一个问题: 消息 10314,级别 16,状态 11,第 1 行 在尝试加载程序集 ID 65536 时 Microsoft ...
- Android文档 学习目录
Building Your First App After you've installed the Android SDK, start with this class to learn the b ...
- OpenCV Python : No drawMatchesknn function
2 down vote The functions cv2.drawMatches and cv2.drawMatchesKnn are not available in newer versions ...
- 通过网址request到response经历的过程
前言当我们在浏览器中输入一个网址,比如www.google.cn,浏览器就会加载出百度的主页.那么浏览器背后完成的具体是怎么样的呢? 总结起来大概的流程是这样的: (1)浏览器本身是一个客户端,当你输 ...
- 1-keystone 部署
https://github.com/openstack/keystone 最新版为rocky 1. 进入mysql create database keystone; grant all privi ...
- quast-lg
1.官网简介 http://cab.spbu.ru/software/quast-lg/ QUAST- lg是QUAST的一个扩展,用于评估大型基因组装配(直至哺乳动物大小).QUAST- lg从5. ...