1、springMVC使用@RequestMapping注解为控制器指定可以处理哪些URL请求
- DispatcherServlet截获请求后就通过控制器上@RequestMapping提供的映射信息确定请求所对应的处理方法
- @RequestMapping不仅可以修饰方法还可以修饰类
1)类定义处:提供初步的请求映射信息,相对于web应用的根目录
2)方法定义处:提供进一步的细分映射信息,相对于类定义处的URL,若类定义处未标记@RequestMapping,则方法标记的URL相对于WEB应用的根目录
1)修饰方法
1 package com.atguigu.springmvc.handlers;
2
3 import org.springframework.stereotype.Controller;
4 import org.springframework.web.bind.annotation.RequestMapping;
5
6 @Controller //这样标识后HelloWorld成为控制器,在springmvc叫做handler处理器或请求处理器
7 public class HelloWorld {
8 /**
9 * 1、使用@RequestMapping注解来映射请求的URL
10 * 2、返回值会通过视图解析器解析为实际的物理视图:对于InternalResourceViewResolver视图解析器,会做如下解析:
11 * 通过prefix+returnvel+后缀这样的方式得到实际的物理视图,然后转发
12 * /WEB-INF/views/success.jsp
13 * @return
14 */
15 @RequestMapping("/helloworld")//映射请求
16 public String hello() {
17 System.out.println("HelloWorld");
18 return "success";
19 }
20 }
实现代码同HelloWorld实例。
2)修饰类
1 package com.atguigu.springmvc.handlers;
2
3 import org.springframework.stereotype.Controller;
4 import org.springframework.web.bind.annotation.RequestMapping;
5
6 @RequestMapping("/springmvc")
7 @Controller
8 public class SpringMVCTest {
9 private static final String SUCCESS = "success";
10
11 @RequestMapping("/testRequestMapping")
12 public String testRequestMapping() {
13 System.out.println("testRequestMapping");
14 return SUCCESS;
15 }
16 }
3)index.xml改为
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <!-- 超链接 --> 11 <a href="/springmvc/testRequestMapping">TestRequestMapping</a> 12 <!-- <br><br> <a href="/helloworld">HelloWorld</a> --> 13 </body> 14 </html>
2、 RequestMapping的请求方式(联合多个条件可以让请求映射更加精确化)
请求URL:value
请求方法:method
请求参数:params(了解)
请求头:heads(了解)
- 举例:method(常用)
1 package com.atguigu.springmvc.handlers;
2
3 import org.springframework.stereotype.Controller;
4 import org.springframework.web.bind.annotation.RequestMapping;
5 import org.springframework.web.bind.annotation.RequestMethod;
6
7 @RequestMapping("/springmvc")
8 @Controller
9 public class SpringMVCTest {
10 private static final String SUCCESS = "success";
11 /**
12 * 使用method属性来指定请求方式
13 */
14 @RequestMapping(value="/testMethod",method=RequestMethod.POST)
15 public String testMethod(){
16 System.out.println("testMethod");
17 return SUCCESS;
18 }
19 }
index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <!-- 超链接 --> 11 <form action="springmvc/testMethod" method="POST"> <input 12 type="submit" value="submit"></form> 13 <a href="springmvc/testMethod">TestMethod</a> 14 <!-- <br><br> <a href="/helloworld">HelloWorld</a> --> 15 </body> 16 </html>
- 举例params
1 package com.atguigu.springmvc.handlers;
2
3 import org.springframework.stereotype.Controller;
4 import org.springframework.web.bind.annotation.RequestMapping;
5 import org.springframework.web.bind.annotation.RequestMethod;
6
7 @RequestMapping("/springmvc")
8 @Controller
9 public class SpringMVCTest {
10 private static final String SUCCESS = "success";
11 /**
12 * 了解:可以使用params和headers来更加精确的映射请求,params和headers支持简单的表达式
13 */
14 @RequestMapping(value="testParamsAndHeaders",
15 params={"username","age!=10"})
16 public String testParamsAndHeaders(){
17 System.out.println("testParamsAndHeaders");
18 return SUCCESS;
19 }
20 }
index.jsp文件
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <!-- 超链接 --> 11 12 <a href="springmvc/testParamsAndHeaders?username=atguigu&age=10">Test ParamsAndHeaders</a> 13 </body> 14 </html>
- 举例headers
1 package com.atguigu.springmvc.handlers;
2
3 import org.springframework.stereotype.Controller;
4 import org.springframework.web.bind.annotation.RequestMapping;
5 import org.springframework.web.bind.annotation.RequestMethod;
6
7 @RequestMapping("/springmvc")
8 @Controller
9 public class SpringMVCTest {
10 private static final String SUCCESS = "success";
11 /**
12 * 了解:可以使用params和headers来更加精确的映射请求,params和headers支持简单的表达式
13 */
14 @RequestMapping(value="testParamsAndHeaders",
15 params={"username","age!=10"},headers={"Accept-Language=zh-CN,zh;q=0.8"})
16 public String testParamsAndHeaders(){
17 System.out.println("testParamsAndHeaders");
18 return SUCCESS;
19 }
20 }
index.jsp与上述相同
3、@RequestMapping映射请求支持Ant风格资源地址3中匹配符:(了解)
- ?:匹配文件名中的一个字符
- * :匹配文件名中的任意字符
- **:**匹配多层路径
1 @RequestMapping("/springmvc")
2 @Controller
3 public class SpringMVCTest {
4 private static final String SUCCESS = "success";
5 @RequestMapping("/testAntPath/*/abc")
6 public String testAntPath(){
7 System.out.println("testAntPath");
8 return SUCCESS;
9 }
来源:https://www.cnblogs.com/wangna----558169/p/6032726.html