学习@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. Node.js+Protractor+vscode搭建测试环境(1)

    1.protractor简介 官网地址:http://www.protractortest.org/ Protractor是一个end-to-end的测试框架,从网络上得到的答案是Protractor ...

  2. Jquery-自定义表单验证

    jQuery自定义表单验证

  3. AtCoder ABC 070D - Transit Tree Path

    传送门:http://abc070.contest.atcoder.jp/tasks/abc070_d 本题是一个图论问题——树(Tree). 有一棵结点数目为n的无向树.第i条边连接结点ai与bi, ...

  4. 洛谷——P1060 开心的金明

    https://www.luogu.org/problem/show?pid=1060#sub 题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的 ...

  5. Spring MVC-表单(Form)标签-文本框(Text Box)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_textbox.htm 说明:示例基于Spring MVC 4.1.6. 以下示例 ...

  6. Qt的Socket数据通讯的一个样例。

    QTcpServer类 用来侦听port ,获取QTcpSocket. QTcpSocket有  connected的信号(已经连接),还有readyread()信号,表示已经有数据发过来了.准备读取 ...

  7. LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)

    和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...

  8. CSS艺术之---负margin之美

    CSS中负边距(nagative margin)是布局中常常使用的一个技巧.仅仅要运用得当时常会产生奇异的效果.勘称CSS中的奇淫巧计,非常多CSS布局方法都依赖于负边距.掌握它对于前端童鞋来说还是非 ...

  9. ueditor1.4.3在.net环境下的vs开发工具中集成经验

    Ueditor是个非常不错的在线富文本编辑器,几个项目一直使用它.近期想更新版本号.发现新版1.4.3与旧版的部署方式全然不一样了.官网文档介绍的是直接放在iis下的部署说明,没有提到在vs开发工具中 ...

  10. 【HAOI 2008】 移动玩具

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1054 [算法] 广度优先搜索 [代码] #include<bits/stdc+ ...