Spring mvc中@RequestMapping 6个基本用法小结

小结下spring mvc中的@RequestMapping的用法。

1)最基本的,方法级别上应用,例如:

  1. @RequestMapping(value="/departments")
  2. public String simplePattern(){
  3. System.out.println("simplePattern method was called");
  4. return "someResult";
  5. }

则访问http://localhost/xxxx/departments的时候,会调用 simplePattern方法了

2) 参数绑定

  1. @RequestMapping(value="/departments")
  2. public String findDepatment(
  3. @RequestParam("departmentId") String departmentId){
  4. System.out.println("Find department with ID: " + departmentId);
  5. return "someResult";
  6. }

形如这样的访问形式:

/departments?departmentId=23就可以触发访问findDepatment方法了

3 REST风格的参数

  1. @RequestMapping(value="/departments/{departmentId}")
  2. public String findDepatment(@PathVariable String departmentId){
  3. System.out.println("Find department with ID: " + departmentId);
  4. return "someResult";
  5. }

形如REST风格的地址访问,比如: 
/departments/23,其中用(@PathVariable接收rest风格的参数

4 REST风格的参数绑定形式之2 
   先看例子,这个有点象之前的:

  1. @RequestMapping(value="/departments/{departmentId}")
  2. public String findDepatmentAlternative(
  3. @PathVariable("departmentId") String someDepartmentId){
  4. System.out.println("Find department with ID: " + someDepartmentId);
  5. return "someResult";
  6. }

这个有点不同,就是接收形如/departments/23的URL访问,把23作为传入的departmetnId,,但是在实际的方法findDepatmentAlternative中,使用 
@PathVariable("departmentId") String someDepartmentId,将其绑定为 
someDepartmentId,所以这里someDepartmentId为23

5 url中同时绑定多个id

  1. @RequestMapping(value="/departments/{departmentId}/employees/{employeeId}")
  2. public String findEmployee(
  3. @PathVariable String departmentId,
  4. @PathVariable String employeeId){
  5. System.out.println("Find employee with ID: " + employeeId +
  6. " from department: " + departmentId);
  7. return "someResult";
  8. }

这个其实也比较好理解了。

6 支持正则表达式

  1. @RequestMapping(value="/{textualPart:[a-z-]+}.{numericPart:[\\d]+}")
  2. public String regularExpression(
  3. @PathVariable String textualPart,
  4. @PathVariable String numericPart){
  5. System.out.println("Textual part: " + textualPart +
  6. ", numeric part: " + numericPart);
  7. return "someResult";
  8. }

比如如下的URL:/sometext.123,则输出: 
Textual part: sometext, numeric part: 123.

Spring mvc中@RequestMapping 6个基本用法小结的更多相关文章

  1. 转:Spring mvc中@RequestMapping 6个基本用法小结

    Spring mvc中@RequestMapping 6个基本用法小结 发表于3年前(2013-02-17 19:58)   阅读(11698) | 评论(1) 13人收藏此文章, 我要收藏 赞3 4 ...

  2. Spring mvc中@RequestMapping 6个基本用法小结(转载)

    小结下spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: @RequestMapping(value="/departments" ...

  3. Spring mvc中@RequestMapping 6个基本用法

    Spring mvc中@RequestMapping 6个基本用法 spring mvc中的@RequestMapping的用法.  1)最基本的,方法级别上应用,例如: Java代码 @Reques ...

  4. Spring mvc中@RequestMapping 6个基本用法整理

    继续整理,这个是前段时间用jsp开发的一个站点,说起来php程序员去做jsp程序确实有些小不适应,但是弄完后绝对对于这种强类型语言而比收获还是颇多的. 1,最基本的,方法级别上应用 @RequestM ...

  5. Spring MVC中@RequestMapping注解使用技巧(转)

    @RequestMapping是Spring Web应用程序中最常被用到的注解之一.这个注解会将HTTP请求映射到MVC和REST控制器的处理方法上. 在这篇文章中,你将会看到@RequestMapp ...

  6. Spring mvc中@RequestMapping 基本用法

    @RequestMapping(value="/departments") public String simplePattern(){ System.out.println(&q ...

  7. Spring MVC中@RequestParam/@RequestBody/@RequestHeader的用法收集(转)

    简介: handler method参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri部分(这里指uri template中 ...

  8. Spring MVC中的ModelMap作用及用法

    ModelMap的作用: ModelMap对象主要用于传递控制方法传递数据到结果页面.类似于request的setAttribute方法的作用. 所以我们要想在jsp页面获取数据,只要将数据放到Mod ...

  9. Spring Mvc中DispatcherServlet和Servlet的区别小结

    在web开发过程中开始接触的是servlet,用来处理用户请求.这几年随着spring 框架越来越成熟,几乎成了java web开发界的主流框架.既然这么受欢迎肯定有它的优点,spring框架在原来的 ...

随机推荐

  1. .NET:CLR via C# Assembly Loading

    基础知识 Internally, the CLR attempts to load this assembly by using the System.Reflection.Assembly clas ...

  2. pytest文档24-fixture的作用范围(scope)

    fixture作用范围 fixture里面有个scope参数可以控制fixture的作用范围:session > module > class > function fixture( ...

  3. [翻译] ClockView 时钟

    ClockView 时钟 https://github.com/nacho4d/ClockView Overview ClockView is s simple class that will sim ...

  4. eclipse3.4配置的tomcat server如何部署以前的web项目?

    1. 打开.project文件,在<natures>元素中加入 <nature>org.eclipse.wst.common.project.facet.core.nature ...

  5. Python for everyone chapter 1

    Chapter 1 10 试题 1.  When Python is running in the interactive mode and displaying the chevron prompt ...

  6. 解决 Ubuntu Software (Software Center) Crash 问题

    问题描述: no application data found 解决方式: sudo apt purge gnome-software ubuntu-software sudo apt autorem ...

  7. 使用Scala

    1. 净资产应用实例 我们要构建这样一个应用,它会取回一份列表,其中包括用户持有的股票的代码以及股份,并告知他们在当前日期为止的这些投资的总价.这包含了几件事:获取用户输入.读文件.解析数据.写文件. ...

  8. iOS开发-简单的图片查看器

    现在你只要拿着手机,不管你Android还是iOS,新闻类的App不可避免都有一个功能就是图片查看,做个专题,查看一下内容,App Store中也有专门针对图片浏览的App,鉴于目前所知有限,无法做到 ...

  9. 合并JavaScript数组的N种方法

    这是一篇简单的文章,关于JavaScript数组使用的一些技巧.我们将使用不同的方法结合/合并两个JS数组,以及讨论每个方法的优点/缺点. 让我们先考虑下面这情况: var a = [ 1, 2, 3 ...

  10. CSS文字换行详细解说

    本文列举了兼容 IE 和 FF 地换行 CSS 推荐样式,详细介绍了word-wrap同word-break地区别.兼容 IE 和 FF 地换行 CSS 推荐样式: 最好地方式是 word-wrap: ...