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="