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

发表于3年前(2013-02-17 19:58)   阅读(11698) | 评论(1) 13人收藏此文章, 我要收藏
赞3

4月23日,武汉源创会火热报名中,期待您的参与>>>>>  

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

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

Java代码  
  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) 参数绑定

Java代码  
  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风格的参数

Java代码  
  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  
   先看例子,这个有点象之前的:

Java代码  
  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

Java代码  
  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 支持正则表达式

Java代码  
  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个基本用法小结 小结下spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: @RequestMa ...

  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中DispatcherServlet和Servlet的区别小结

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

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

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

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

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

随机推荐

  1. AGC007 - C Pushing Ball

    Description 题目链接 懒得写详细题意了, 放个链接 \(n\le 2*10^5\) 个球, \(n+1\) 个坑, 排成数轴, 球坑交替. 相邻球-坑距离为等差数列 \(d\). 给定首项 ...

  2. 也来写写基于单表的Orm(使用Dapper)

    前言 这两天看园子里有个朋友写Dapper的拓展,想到自己之前也尝试用过,但不顺手,曾写过几个方法来完成自动的Insert操作.而对于Update.Delete.Select等,我一直对Diction ...

  3. oracle 的数组(转)

    declare type t_indexby is table of number index by binary_integer; type t_nested is table of number; ...

  4. 杭电oj2012-2021

    2012  素数判定 #include <stdio.h> #include <math.h> int main() { int x,y,i,j,a,flag; while(s ...

  5. List<?>和List<Class<?>>区别及泛型相关

    ?表示是任意类型,但是编译器不能确定他是什么类型,所以你add的时候什么参数也不能传给它Class<?>表示任意类型的Class对象,list里面可以添加任何类型的Class对象,其它的对 ...

  6. 使用bottle进行web开发(3):静态文件的获取

    静态文件(比如css啊,需要下载的各位文件等),需要通过static_file来操作,首先记得要在import中导入 @route('/static/<filename>') def se ...

  7. 使用overtrue/socialite实现第三方登陆

    composer下载包 将申请的配置内容放在.ENV文件中 在services.php文件中引用 控制器 其他第三方登陆同理,拿到client_id,client_secret 和redirect_u ...

  8. JAVA SERVLET上传文件的样码

    import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import j ...

  9. 在16aspx.com上下了一个.net程序,怎么修改它的默认登录名和密码?

    正常应该有个login.aspx文件,找到登入按钮事件,查看验证用户名密码事件,这个验证就有去搜索用户表,然后在这个用户表修改用户名密码就行了.不排除其他表有用用户名关联

  10. 计蒜客 28437.Big brother said the calculation-线段树+二分-当前第k个位置的数 ( ACM训练联盟周赛 M)

    M. Big brother said the calculation 通过线段树维护. 这个题和杭电的一道题几乎就是一样的题目.HDU5649.DZY Loves Sorting 题意就是一个n的排 ...