@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 ...
随机推荐
- SSM商城项目(十三)
1. 学习计划 1.订单系统 2.提交订单 3.MyCAT 2. 订单系统 2.1. 功能分析 1.在购物车页面点击“去结算”按钮跳转到订单确认页面. a) 展示商品列表 b) ...
- eclipse打包java项目
参考链接:https://blog.csdn.net/heshushun/article/details/78039801
- 3G设置linux路由-iptables配置
1.如何区分iptables的PREROUTING和POSTROUTING链 (引自http://jingyan.baidu.com/article/aa6a2c143d84470d4c19c4cf. ...
- 机器学习--Xgboost调参
Xgboost参数 'booster':'gbtree', 'objective': 'multi:softmax', 多分类的问题 'num_class':10, 类别数,与 multisoftma ...
- git 免密码push
git版本2.14.1 windows系统 用户根目录 .gitconfig 文件添加配置 [credential] helper = store[push] default = simple 用户根 ...
- project4 安卓
[MongoDB的密码] 用户名dbUser 密码dbUserPassword00 dbUser:dbUserPassword00 [概念] POM的配置要严格按照网页指示给出的标签顺序来. 安卓AP ...
- Smartforms
Include text Populate indicator in program perform get_text using '0002' ls_detail-vbeln"Header ...
- supervisor学习
概念 supervisord 服务端 启动supervisor程序自身,启动supervisor管理的子进程,响应supervisorctl的请求,重启闪退或异常退出的子进程,把子进程的stderr或 ...
- JavaSE基础知识(5)—面向对象(5.6 static关键字)
一.说明 static属于一种修饰符,可以用于修饰 属性.方法.初始化块.内部类用static修饰的成员,称为静态成员不用static修饰的成员,称为普通成员 二.静态属性的特点 1.生命周期 静态属 ...
- c#跨线程访问的代码和窗体关闭退出死循环的代码
一:一段跨线程访问,给页面内的控件赋值的代码找了半天没找到,还得找了以前写的程序. 在这记下来吧 . 这是其他程序内可以跨线程访问的代码 . if (gridControl1.InvokeRequi ...