@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下都存在这一问题). ...
随机推荐
- 3.有关于Python列表简述
一..title() [让所选择的列表元素的第一个字母大写] test = ['no1','No2','No3','No4'] book = "This My " + test[0 ...
- zabbix server配置文件参数详解
AlertScriptsPath默认值:/usr/local/share/zabbix/alertscripts说明:告警脚本目录 AllowRoot默认值:0 说明:是否允许使用root启动,0:不 ...
- Java IO流学习总结七:Commons IO 2.5-FileUtils
在上面的几篇文章中,介绍了IO的常规用法,今天介绍 Commons IO 框架的使用. Commons IO简介 Apache Commons IO是Apache基金会创建并维护的Java函数库.它提 ...
- WebStorm新创建项目介绍
WebStorm创建一个项目 这里支持有很多的类型项目: Empty Project ----一个空的项目 Html5 Boilerplate ----HTML5开发框架 We ...
- KVM虚拟化技术(三)KVM环境预配
一.平台操作系统安装 选择合适的操作系统,此处选用CentOS 7 系统可最小化安装,也可标准安装: 如果要远程连接,建议安装VNC-Server 将防火墙配置可通信,SELINUX设为permiss ...
- JAVAWEB 一一ibatis(框架)
,升级版是mybatis,在配置文件里写sql语句对字段进行CURD) jar包 sqlMapConfig <?xml version="1.0" encoding=&quo ...
- C# 图像处理:复制屏幕到内存中,拷屏操作
/// <summary> /// 复制屏幕到内存中 /// </summary> /// <returns>返回内存流</returns> publi ...
- C# 获取汉字转拼音缩写-简写,不是全拼
///<summary> /// 汉字转拼音缩写 /// Code By ] -'\0')); if ( i <0xB0A1) return"*" ...
- Android代码规范
Android代码规范——文章来源<IT蓝豹>http://itlanbao.com/preview.aspx#1,0 [-]一Import的次序二缩进Indentation总则示例代码规 ...
- java的acm输入输出格式+大数语法
1.类名称必须采用public class Main方式命名 2.多组输入,读取到文件尾 Scanner scan=new Scanner(System.in); while(scan.hasNext ...