设为首页 加入收藏

TOP

spring-mvc系列:详解@RequestMapping注解(value、method、params、header等)(一)
2023-08-06 07:49:50 】 浏览:57
Tags:spring-mvc 系列 详解 @RequestMapping 注解 value method params header

一、@RequestMapping注解的功能

从注解名称上我们可以看到,@RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来,建立映射关系。

SpringMVC 接收到指定的请求,就会来找到在映射关系中对应的控制器方法来处理这个请求。

二、@RequestMapping注解的位置

@RequestMapping标识一个类:设置映射请求的请求路径的初始信息

@RequestMapping标识一个方法:设置映射请求请求路径的具体信息

package com.mcode.api.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * ClassName: RequestMappingController
 * Package: com.mcode.api.controller
 * Description:
 *
 * @Author: robin
 * @Create: 2023/8/3 - 10:32 PM
 * @Version: v1.0
 */
@Controller
@RequestMapping("/test")
public class RequestMappingController {

    //此时请求映射所映射的请求的请求路径为:/test/testRequestMapping 
    @RequestMapping("/testRequestMapping")
    public String testRequestMapping() {
        return "success";
    }
}

三、@RequestMapping注解的value属性

@RequestMapping注解的value属性通过请求的请求地址匹配请求映射

@RequestMapping注解的value属性是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址所对应的请求

@RequestMapping注解的value属性必须设置,至少通过请求地址匹配请求映射

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        a{
          display: block;
        }
    </style>
</head>
<body>
   <a th:href="@{/testRequestMapping}">测试@RequestMapping的value属性-->/testRequestMapping</a>
   <a th:href="@{/test}">测试@RequestMapping的value属性-->/test</a>
</body>
</html>
package com.mcode.api.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * ClassName: RequestMappingController
 * Package: com.mcode.api.controller
 * Description:
 *
 * @Author: robin
 * @Create: 2023/8/3 - 10:32 PM
 * @Version: v1.0
 */
@Controller
//@RequestMapping("/test")
public class RequestMappingController {

    @RequestMapping(value = {"test", "testRequestMapping"})
    public String testRequestMapping() {
        return "success";
    }
}

四、@RequestMapping注解的method属性

@RequestMapping注解的method属性通过请求的请求方式(get或post)匹配请求映射

@RequestMapping注解的method属性是一个RequestMethod类型的数组,表示该请求映射能够匹配多种请求方式的请求,若当前请求的请求地址满足请求映射的value属性,但是请求方式不满足method属性,则浏览器报错

405:Request method 'POST' not supported

image

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        a{
          display: block;
        }
    </style>
</head>
<body>
   <a th:href="@{/testRequestMapping}">测试@RequestMapping的value属性-->/testRequestMapping</a>
   <a th:href="@{/test}">测试@RequestMapping的value属性-->/test</a>
   <form th:action=&qu
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇quarkus依赖注入之六:发布和消费.. 下一篇RabbitMQ延迟队列,死信队列配置

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目