@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的更多相关文章

  1. @RequestParam 和 @ PathVariable 的区别

    @RequestParam 和 @ PathVariable 的区别http://localhost:8080/Springmvc/user/page.do?pageSize=3&pageNo ...

  2. 【转】@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    @RequestParam @RequestBody @PathVariable 等参数绑定注解详解 2014-06-02 11:24 23683人阅读 评论(2) 收藏 举报 目录(?)[+] 引言 ...

  3. 浅谈 @RequestParam 和@PathVariable

    版权声明:本文为博主原创文章,如果对你有用,敬请带走! https://blog.csdn.net/chuck_kui/article/details/55506723 首先 上两个地址: 地址①ht ...

  4. @RequestParam和@PathVariable的区别及其应用场景

    @RequestParam和@PathVariable这两者之间区别不大,主要是请求的URL不一样 用@RequestParam请求接口时,URL是:http://www.test.com/user/ ...

  5. @RequestParam,@PathVariable等注解区别

    一.@RequestParam和@PathVariable的区别 1.@RequestParam是从uri中request后面的参数串来取得参数的 2.@PathVariable是从uri模板中取得参 ...

  6. @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    文章主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用. 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...

  7. Spring @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    背景 昨天一个人瞎倒腾spring boot,然后遇到了一点问题,所以把这个问题总结一下. 主要讲解request 数据到handler method 参数数据的绑定,所用到的注解和什么情形下使用. ...

  8. springMVC的注解@RequestParam与@PathVariable的区别

    1.在SpringMVC后台控制层获取参数的方式主要有两种, 一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取. ...

  9. @RequestParam与@PathVariable的区别

    在spring MVC中,两者的作用都是将request里的参数的值绑定到contorl里的方法参数里的,区别在于,URL写法不同. 使用@RequestParam时,URL是这样的:http://h ...

随机推荐

  1. nginx warn an upstream response is buffered to a temporary file /var/cache/nginx/proxy_temp/ while reading upstream

    最近管理的nginx发现大量的error log,log内容如下: an upstream response is buffered to a temporary file /var/cache/ng ...

  2. WCF输出JSON

    public class MyService : IService { public Message GetXml(string format) { WebOperationContext conte ...

  3. MFC列表控件更改一行的字体颜色

    参考自(http://blog.csdn.net/ribut9225/article/details/6720639) 1.首先从CListCtrl 继承一个类,命名为CListCtrlCl 在头文件 ...

  4. event.keyCode用法及列表

    HTML 用户名:<input type="text" id="UserAccount" onKeyPress="JumpByEnter(Use ...

  5. 【IDEA】关于idea开发工具使用

    idea这个开发工具有很强大的功能,然鹅有时候某些地方需要注意...(注明一下,我的版本2017.3.2因为不知道新版本有木有优化) 先说说建包,比如我在src下建了个com.xxxxx.test的包 ...

  6. Appium 学习三:Appium-Desktop 填坑

    之前使用appium执行自动化脚本,现使用appium-desktop  遇到了一些问题 1.使用name定位的报错   Locator Strategy 'name' is not supporte ...

  7. centos7更改时区

    1,linux系统更换时区 2,时间同步 1,linux系统更换时区 注:# timedatectl # 查看系统时间方面的各种状态 # timedatectl list-timezones # 列出 ...

  8. 了解计算机与操作系统发展阶段--Windows

    Windows发展的30多年,其实就是整个计算机应用,从小众化向大众化消费领域,快速前行的30多年. 让我们来一起温故下Windows这么多年的发展历程,看看Windows,是如何在市场和技术这两种力 ...

  9. 计算C#程序执行时间

    static void SubTest()   {       DateTime beforDT = System.DateTime.Now;            //耗时巨大的代码         ...

  10. Win10下windows mobile设备中心连接不上的方法无法启动

    微软Win10自动更细补丁后windows mobile设备中心就无法启动了 需要重新启动相关的服务并授予 本机登录用户 权限 1.点击屏幕左下角“开始”图标,点击“运行”,在弹出的输入框中输入“se ...