SpringMVC(03) -- @RequestMapping注解

SpringMVC学习笔记

源码地址

三、@RequestMapping注解

3.1)@RequestMapping注解的功能

从注解名称上可以看到,@RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来建立映射关系;SpringMVC 接收到指定的请求,就会来找到在映射关系中对应的控制器方法来处理这个请求。

如上述步骤建立工程 SpringMvcDemo2,新建 TestController.java

@Controller
public class TestController {
    @RequestMapping("/")
    public String index() {
        return "index";
    }
}

新建 RequestMappingController.java

public class RequestMappingController {
    @RequestMapping("/")
    public String index() {
        return "index";
    }
}

启动工程报错:【保证在所有控制器中@RequestMapping对于的路径是唯一的,否则SpringMVC 不知道该匹配哪一个路径报错】

12:47:53.487 [http-nio-8080-exec-3] WARN org.springframework.web.context.support.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'testController' method com.study.mvc.controller.TestController#index() to { [/]}: There is already 'requestMappingController' bean method com.study.mvc.controller.RequestMappingController#index() mapped. 12:47:53.489 [http-nio-8080-exec-3] ERROR org.springframework.web.servlet.DispatcherServlet - Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'testController' method com.study.mvc.controller.TestController#index() to { [/]}: There is already 'requestMappingController' bean method com.study.mvc.controller.RequestMappingController#index() mapped.

3.2)@RequestMapping注解的位置

@RequestMapping注解源码如下:

@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE, java.lang.annotation.ElementType.METHOD})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Documented
@org.springframework.web.bind.annotation.Mapping
public @interface RequestMapping {
    ...
}

以下代码说明可以@RequestMapping可以标识类和方法

@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE, java.lang.annotation.ElementType.METHOD})
  • @RequestMapping标识一个类:设置映射请求的请求路径的初始信息

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

代码示例:

@Controller
@RequestMapping("/hello")
public class RequestMappingController {
​
    @RequestMapping("/testRequestMapping")
    public String success(){
        return "success";
    }
}

输出:【配置Tomacat后启动工程,浏览器访问:http://localhost:8080/SpringMvcDemo2/ 】页面如下:

点击超链接,页面跳转至http://localhost:8080/SpringMvcDemo2/hello/testRequestMapping ,页面如下:

 通过在类上设置@RequestMapping,就可以在不同的控制器(模块)上使用相同的路径了

3.3)@RequestMapping注解的value属性

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

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

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

代码示例:

@Controller
@RequestMapping("/hello")
public class RequestMappingController {
​
    @RequestMapping(value = {"/testRequestMapping", "/test"})
    public String success(){
        return "success";
    }
}

index.html 如下:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值