学习@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. 使用ajax爬取网站图片()

    以下内容转载自:https://www.makcyun.top/web_scraping_withpython4.html 文章关于网站使用Ajaxj技术加载页面数据,进行爬取讲的很详细 大致步骤如下 ...

  2. sql server备份策略

    https://www.cnblogs.com/fengzongming/archive/2018/08/29/9530616.html

  3. 重命名文件及html

    import os import nltk from bs4 import BeautifulSoup as bs def get_txt_name_from_bak_name(bak_name): ...

  4. 基于Mysql-Proxy 实现MariaDB 读写分离

    一.Mysql-Proxy 简单介绍 MySQL-Proxy是一个处于你的client端和MySQL server端之间的简单程序,它可以监测.分析或改变它们的通信.它使用灵活,没有限制,常见的用途包 ...

  5. 转载 - AC自动机算法

    出处:http://blog.csdn.net/niushuai666/article/details/7002823 AC自动机简介:  首先简要介绍一下AC自动机:Aho-Corasick aut ...

  6. Set Time, Date Timezone in Linux from Command Line or Gnome | Use ntp

    https://www.garron.me/en/linux/set-time-date-timezone-ntp-linux-shell-gnome-command-line.html Set ti ...

  7. [bzoj3943][Usaco2015 Feb]SuperBull_Kruskal

    SuperBull bzoj-3943 Usaco-2015 Feb 题目大意:贝西和她的朋友们在参加一年一度的“犇”(足)球锦标赛.FJ的任务是让这场锦标赛尽可能地好看.一共有N支球队参加这场比赛, ...

  8. 洛谷 P1560 [USACO5.2]蜗牛的旅行Snail Trails(不明原因的scanf错误)

    P1560 [USACO5.2]蜗牛的旅行Snail Trails 题目描述 萨丽·斯内尔(Sally Snail,蜗牛)喜欢在N x N 的棋盘上闲逛(1 < n <= 120). 她总 ...

  9. nginx 、tomcat 集群配置、shiro Session 共享

    一.nginx.config 配置 #user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error. ...

  10. 配置db账号和密码时一定注意空格问题、空行问题否则连接报错

    #postgresql dbpg.datasource.type=com.alibaba.druid.pool.DruidDataSourcepg.datasource.driverClassName ...