学习@RequestMapping注解,参考Spring API

1.@RequestMapping可以修饰在类类型和方法上
      ①.修饰在类定义上:   提供初步的URL映射,相对于web应用根目录。
      ②.修饰在方定义法上:  提供更细致的URL映射,若类定义上有注解,则相对于类定义上的URL映射。否则相对于web应用根目录映射

代码1:

只在方法上加@RequestMapping:

   1: @Controller

   2: public class TestRequestMapping {

   3:     @RequestMapping("/testMethod")

   4:     public String testMethod() {

   5:         System.out.println("testMethod");

   6:         return "success";

   7:     }

   8: }

URL:

   1: <a href="testMethod">testMethod</a>

代码2:

在类和方法上加@RequestMapping:

   1: @RequestMapping("/testClass")

   2: @Controller

   3: public class TestRequestMapping {

   4:     @RequestMapping("/testMethod")

   5:     public String testClassAndMethod() {

   6:         System.out.println("testClassAndMethod");

   7:         return "success";

   8:     }

   9: }

URL:

   1: <a href="testClass/testMethod">testClassAndMethod</a>

代码3:

只在类上加@RequestMapping:

   1: @RequestMapping("/testClass")

   2: @Controller

   3: public class TestRequestMapping {

   4:  

   5:     public String testClassAndMethod() {

   6:         System.out.println("testClass");

   7:         return "success";

   8:     }

   9: }

URL:

   1: <a href="testClass">testClass</a>

运行时,发出Tomcat警告: No mapping found for HTTP request with URI [/mvc02/testClass] in DispatcherServlet with name 'dispatcherServlet'。

也就说在类上加注解后,必须在方法上也加注解。查看api,发现开头第一句 Annotation for mapping web requests onto specific handler classes and/or handler methods

好吧没仔细看api,classes and/or handler methods。(⊙﹏⊙)b

2.@RequestMapping有7个参数,value,method,headers,params之间是与的关系:

String[]        value URL路径:"/myPath/myMethod"
RequestMethod[]  method 请求方式:GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE
String[]   headers 请求头,用法与params类似,支持简单表达式
String[] params 请求参数,支持简单表达式
      params={"name"}:参数中必须包含名为name的参数
      params={"!name"}:参数中不能包含名为name的参数
      params={"name!=xx"}:参数中若包含了名为name的参数,
                    则name!=xx。参数中也可以不包含名为name的参数
      params={"name=xx"}:参数中必须包含名为name的参数,
                    且name=xx。
String[] produces 指定哪些媒体类型可以不需要
String[] consumes 指定哪些媒体类型可以额外 添加
String name 映射名称

method——代码:

   1: @RequestMapping("/testClass")

   2: @Controller

   3: public class TestRequestMapping {

   4:     @RequestMapping(value = "/testMethod_GET", method = RequestMethod.GET)

   5:     public String testMethod_GET() {

   6:         System.out.println("method running....");

   7:         return "success";

   8:     }

   9:  

  10:     @RequestMapping(value = "/testMethod_POST", method = RequestMethod.POST)

  11:     public String testMethod_POST() {

  12:         System.out.println("method running....");

  13:         return "success";

  14:     }

  15:  

  16: }

URL:

   1: <form action="testClass/testMethod_POST" method="post">

   2:     <input type="submit" value="testMethod_POST"/>

   3: </form>

   4: <br/><br/>

   5:  

   6: <a href="testClass/testMethod_GET">testMethod_GET</a>

SpringMVC(二)@RequestMapping的更多相关文章

  1. SpringMVC(二):RequestMapping修饰类、指定请求方式、请求参数或请求头、支持Ant路径

    @RequestMapping用来映射请求:RequestMapping可以修饰方法外,还可以修饰类 1)SpringMVC使用@RequestMapping注解为控制指定可以处理哪些URL请求: 2 ...

  2. SpringMVC 学习笔记(二) @RequestMapping、@PathVariable等注解

    版权声明:本文为博主原创文章,博客地址:http://blog.csdn.net/a67474506?viewmode=contents 1.1. @RequestMapping映射请求 Spring ...

  3. SpringMVC注解@RequestMapping之produces属性导致的406错误

    废话不多说,各位,直接看图说话,敢吗?这个问题网上解决的办法写的狠是粗糙,甚至说这次我干掉它完全是靠巧合,但是也不否认网上针对406错误给出的解决方式,可能是多种情况下出现的406吧?我这次的流程就是 ...

  4. springMVC(1)---@RequestMapping详解

    @RequestMapping详解 RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径.这句话,太熟悉了.   ...

  5. SpringMVC(三) RequestMapping修饰类

    SpringMVC使用@RequestMapping 注解为控制器指定可以处理哪些URL请求. 可以用于类定义以及方法定义: 类定义:提供初步的请求映射信息.相对于WEB应用的根目录. 方法处:提供进 ...

  6. SpringMVC(十二) RequestMapping使用POJO作为参数

    将一个普通的JAVA类对象作为一个参数传入. POJO类Address: package com.tiekui.springmvc.pojo; public class Address { priva ...

  7. SpringMVC中 -- @RequestMapping的作用及用法

    一.@RequestMapping 简介 在Spring MVC 中使用 @RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求,相当于Servlet中在web.x ...

  8. SpringMVC的@RequestMapping和Controller方法返回值

    本节内容: @RequestMapping Controller方法返回值 一.@RequestMapping 通过@RequestMapping注解可以定义不同的处理器映射规则. 1. URL路径映 ...

  9. SpringMVC注解@RequestMapping @RequestParam @ResponseBody 和 @RequestBody 解析

    SpringMVC Controller层获取参数及返回数据的方式: @RequestMapping @RequestMapping(“url”),这里的 url写的是请求路径的一部分,一般作用在 C ...

  10. 【SpringMVC】---RequestMapping、Ant 路径、PathVariable 注解、HiddenHttpMethodFilter 过滤器、用 POJO 作为参数

    一.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...

随机推荐

  1. IDEA返回上一步

    在开发中进入一个方法后想要到原来那行 ctrl+alt+左 回到上一步 ctrl+alt+右 回到下一步

  2. Asp.Net使用Yahoo.Yui.Compressor.dll压缩Js|Css

    网上压缩css和js工具很多,但在我们的系统中总有特殊的地方.也许你会觉得用第三方的压缩工具很麻烦.我就遇到了这样问题,我不想在本地压缩,只想更新到服务器上去压缩,服务器压缩也不用备份之类的操作.于是 ...

  3. 模型概念--MVC-MVVM

    MVVM 第一个M是数据访问曾,第二个v是view视图页面,第三个vm是ViewModel视图模型

  4. 在AIX下面查询上一次命令

    在AIX下面查询上一次命令 输入 r 或者 set -o vi 用vi的操作找上一次命令: 学习了: http://blog.itpub.net/66634/viewspace-1000843/ ht ...

  5. 2.【SELinux学习笔记】概念

    1.强制类型的安全上下文     在SELinux中,訪问控制属性叫做安全上上下文.不管主体还是客体都有与之关联的安全上下文.通常安全上下文是由三部分组成:用户:角色:类型. 如: $id -Z  j ...

  6. javascript中的Base64.UTF8编码与解码详解

    javascript中的Base64.UTF8编码与解码详解 本文给大家介绍的是javascript中的Base64.UTF8编码与解码的函数源码分享以及使用范例,十分实用,推荐给小伙伴们,希望大家能 ...

  7. camera table表编译

    mmm -j8 vendor/mediatek/proprietary/hardware/mtkcam/v1/common/paramsmgr/ 2>&1 | tee ft.lib.lo ...

  8. php中一个经典的!==的用法

    php中一个经典的!==的用法 <?php $str = 'Every time you bleed for reaching greatness.'; $cha = 'E'; if(strpo ...

  9. (hdoj 5137 floyd)How Many Maos Does the Guanxi Worth

    How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/5 ...

  10. 对象设计解耦的方法IOC和DI

    耦合关系不仅会出现在对象与对象之间,也会出现在软件系统的各模块之间,以及软件系统和硬件系统之间.如何降低系统之间.模块之间和对象之间的耦合度,是软件工程永远追求的目标之一.为了解决对象之间的耦合度过高 ...