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. Spring3.1.2与Hibernate4.1.8整合

    整合Spring3.1.2 与 Hibernate 4.1.8 首先准备整合jar: Spring3.1.2: org.springframework.aop-3.1.2.RELEASE.jar or ...

  2. 网页IE轻松调用VLC播放器实现监控(组件+方法大全)【转】

    公司突发奇想,要把刚买回来的网络监控机用自己内部网站在线监控. 作为网站的开发员,我接下了这个任务. 网络上有很多资料参与,但是都不全都不尽人意,最后经过多次的不同关键字的查找和测试,总算让我成功了. ...

  3. PowerDesigner导入SQL脚本

    方法/步骤     打开PowerDesigner,鼠标单击File菜单:   选择:Reverse Enginer,然后在他的子菜单选择Database...;   选择好DBMS(数据库管理系统) ...

  4. 新鲜出炉!9个超高分辨率的iPhone 6原型素材打包下载

    iPhone 6 出场,设计师又有得忙活了,但是新鲜的资源你们在哪里?!今天我们收集了一组精致的iPhone 6 模型素材,超高分辨率,多种视图,全都打包完毕,点一下就可以拿回家!赶紧来取吧!——   ...

  5. C#匿名方法与Delegate类型转换错误

    问题描述 C#2.0出现了匿名方法, 这在一定程度上节省了我们维护代码上下文的精力, 也不需要思考为某个方法取什么名字比较合适. 在FCL的一些方法中要求传入一个Delegate类型的参数, 比如Co ...

  6. Android6.0执行时权限解析,RxPermissions的使用,自己封装一套权限框架

    Android6.0执行时权限解析,RxPermissions的使用.自己封装一套权限框架 在Android6.0中,新添加了一个执行时的权限,我相信非常多人都已经知道了.预计也知道怎么用了,这篇博客 ...

  7. [Bash] Create Aliases in .bash_profile for Common Bash Commands

    .bash_profile is a file that bash invokes (or more technically sources) before the start of a new ba ...

  8. 【iOS】网络操作与AFNetworking

    众所周知.苹果搞的一套框架NSContention发送请求与接收请求的方式十分繁琐.操作起来非常不方便. 不仅要做区分各种请求设置各种不同的參数,并且还要常常在多线程里操作,同一时候还要对请求与返回的 ...

  9. spring源代码系列(一)sring源代码编译 spring源代码下载 spring源代码阅读

    想对spring框架进行深入的学习一下,看看源码,提升和沉淀下自己,工欲善其事必先利其器,还是先搭建好开发环境吧. 环境搭建 sping源代码之前是svn管理,如今已经迁移到了github中了.新版本 ...

  10. Power Network (poj 1459 网络流)

    Language: Default Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23407   ...