@RequestParam与@PathVariable
@PathVariable
带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义
通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable("xxx") 绑定到操作方法的入参中。
/**
* localhost:8080/springmvc/hello/pathVariable/bigsea
* localhost:8080/springmvc/hello/pathVariable/sea
* 这些URL 都会 执行此方法 并且将 <b>bigsea</b>、<b>sea</b> 作为参数 传递到name字段
* @param name
* @return
*/
@RequestMapping("/pathVariable/{name}")
public String pathVariable(@PathVariable("name")String name){
System.out.println("hello "+name);
return "helloworld";
}
JSP(这里指定全路径):
<h1>pathVariable</h1>
<a href="${pageContext.request.contextPath}/hello/pathVariable/bigsea" > name is bigsea </a>
<br/>
<a href="${pageContext.request.contextPath}/hello/pathVariable/sea" > name is sea</a>
<br/>
运行结果:
hello bigsea
hello sea
@RequestParam
@RequestParam 绑定请求参数
在处理方法入参处使用 @RequestParam 可以把请求参数传递给请求方法
– value:参数名
– required:是否必须。默认为 true, 表示请求参数中必须包含对应的参数,若不存在,将抛出异常
/**
* 如果 required = true 则表示请求参数对应的 字段 必须存在.如果不存在则会抛出异常<br/>
* @param firstName 可以为null
* @param lastName 不能为null .为null报异常
* @param age age字段表示如果没有 age 参数 则默认值为 0
* @return
*/
@RequestMapping("/requestParam")
public String requestParam(@RequestParam(value="firstName",required=false)String firstName,
@RequestParam( value="lastName" ,required = true) String lastName,
@RequestParam(value="age",required = false ,defaultValue="0")int age) {
System.out.println("hello my name is " + (firstName == null ? "" : firstName)
+ lastName + "," + age +" years old this year");
return "helloworld";
}
Jsp:
<a href="requestParam?firstName=big&lastName=sea" > name is bigsea , age is 0 </a>
<br/>
<a href="requestParam?lastName=sea&age=23" > name is sea , age is 23 </a>
<br/>
<a href="requestParam" > throws exception </a>
运行结果:
hello my name is bigsea,0 years old this year
hello my name is sea,23 years old this year
@RequestParam与@PathVariable的更多相关文章
- @RequestParam 和 @ PathVariable 的区别
@RequestParam 和 @ PathVariable 的区别http://localhost:8080/Springmvc/user/page.do?pageSize=3&pageNo ...
- 【转】@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
@RequestParam @RequestBody @PathVariable 等参数绑定注解详解 2014-06-02 11:24 23683人阅读 评论(2) 收藏 举报 目录(?)[+] 引言 ...
- 浅谈 @RequestParam 和@PathVariable
版权声明:本文为博主原创文章,如果对你有用,敬请带走! https://blog.csdn.net/chuck_kui/article/details/55506723 首先 上两个地址: 地址①ht ...
- @RequestParam和@PathVariable的区别及其应用场景
@RequestParam和@PathVariable这两者之间区别不大,主要是请求的URL不一样 用@RequestParam请求接口时,URL是:http://www.test.com/user/ ...
- @RequestParam,@PathVariable等注解区别
一.@RequestParam和@PathVariable的区别 1.@RequestParam是从uri中request后面的参数串来取得参数的 2.@PathVariable是从uri模板中取得参 ...
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
文章主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用. 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...
- Spring @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
背景 昨天一个人瞎倒腾spring boot,然后遇到了一点问题,所以把这个问题总结一下. 主要讲解request 数据到handler method 参数数据的绑定,所用到的注解和什么情形下使用. ...
- springMVC的注解@RequestParam与@PathVariable的区别
1.在SpringMVC后台控制层获取参数的方式主要有两种, 一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取. ...
- @RequestParam与@PathVariable的区别
在spring MVC中,两者的作用都是将request里的参数的值绑定到contorl里的方法参数里的,区别在于,URL写法不同. 使用@RequestParam时,URL是这样的:http://h ...
随机推荐
- jdk-8u181-docs.chm -- 制作时间2018年8月12日
为了方便查阅,自己做了一个JDK8的chm文件:jdk-8u181-docs.chm 密码: g675 chm制作工具 :chmwriter 目录:
- cron表达式学习
一.Cron 是计划任务(定时任务) 二.Cron表达式 Cron表达式是一个字符串,分为6或7个域,每个域用空格分开.Cron有如下两种语法格式: (1) Seconds Minutes Hours ...
- mapreduce 内存分配
稍微有点mapreduce使用经验的同学肯定对OOM不陌生,对的,我目前在mapReduce里面遇到的最多的报错也是内存分配出错,所以看到好多hadoop执行脚本里面有好多关于内存的参数,虽然是知道和 ...
- python第一周语言基础
控制语句 if语句,当条件成立时运行语句块.经常与else, elif(相当于else if) 配合使用. for语句,遍历列表.字符串.字典.集合等迭代器,依次处理迭代器中的每个元素. while语 ...
- python 并发编程 操作系统 进程 并发.并行 及 同步/异步,阻塞/非阻塞
操作系统: 多道技术背景: 提高工作效率(充分利用IO阻塞的时间) 同时执行多个任务 多道技术 空间复用:充分的利用内存空间 时间复用:充分利用IO阻塞时间 分时系统: 并发:提高了程序的工作效率 两 ...
- Mysql数据存在更新,不存在则插入的实现
有些情况下,我们需要对数据库进行插入或更新操作的时候需要先根据特定的字段去判断是否已存在,然后做出不同的插入或者跟新操作. replace函数: 我们要在需要判断的字段上建立一个唯一索引,且这个字段不 ...
- SpringMVC中注解@RequestBody和@ResponseBody的使用区别
首先上源码 在面试时经常会问到我们如何使用SpringMVC将Http请求转换为java对象,或者又是问如何将结果转换为java的呢? SpringMVC在接收到请求之后HandlerMapping像 ...
- XACT_ABORT选项
XACT_ABORT选项用于指定当SQL语句出现运行时错误时,SQL Server是否自动回滚到当前事务.其语法格式如下所示: SET XACT_ABORT{ON|OFF} 当SET XACT_ABO ...
- c#: 创建桌面快捷方式
App适配输出方式时发现问题,聊做备忘. 需要注意的是:不要直接引用Interop.IWshRuntimeLibrary.dll程序集,因为它可能是x86或x64的,倘若程序以Any CPU方式编译, ...
- golang 安装tensorflow
TF_TYPE="cpu" # Change to "gpu" for GPU support //设置环境变量 TARGET_DIRECTORY='/u ...