SpringMVC (二)测试@RequestMapping
Spring MVC 的 @RequestMapping 注解
- @RequestMapping 既可以修饰类,也可以修饰方法
– 修饰 Class 类:提供初步映射信息
– 修饰 Method 方法:提供细化的映射信息
若类 Class 未标注 @RequestMapping,则方法处标记的 URL 相对于
WEB 应用的根目录 - @RequestMapping 的 value、method、params 及 heads
分别表示请求 URL、请求方式(POST PUT)、请求参数及请求头的映射条
件,他们之间是与的关系,联合使用多个条件可让请求映射
更加精确化。
测试 @RequestMapping 的 method
结构如下:
HelloWorld.java 如下(测试 method=RequestMethod.POST)):
package com.atguigu.springmvc.handlers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RequestMapping(value = "/springmvc" ,method=RequestMethod.POST)
@Controller
public class HelloWorld {
@RequestMapping(value = "/springmvc",method= RequestMethod.POST)
public String hello() {
System.out.println("hello world");
return "success2";
}
}
SpringMVC.xml(设置后台访问路径前缀、后缀)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.atguigu.springmvc"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index.jsp</title>
</head>
<body>
<a href="springmvc/springmvc">success page.</a>
<br/><br/>
<form action="springmvc/springmvc" method="POST">
<input type="submit" value="submit"/>
</form>
<br/><br/>
</body>
</html>
views文件夹下的 success2.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>success page</title>
</head>
<body>
<h1>success!</h1>
</body>
</html>
结果:
点击 success page.
点击 submit
结论:
在 HelloWorld.java 类中写 @RequestMapping(value = "/springmvc" ,method=RequestMethod.POST),请求由默认的 get 变为 post
测试 @RequestMapping 的 params
对上述 HelloWorld.java 进行修改:
package com.atguigu.springmvc.handlers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping(value = "/springmvc" ,params={"username","age!=10"})
//@RequestMapping(value = "/springmvc" ,params={"username","age!=10"},headers={"Accept-language=en-US,zh;q=0.8"})
//@RequestMapping(value = "/springmvc" ,params={"username","age!=10"})
//@RequestMapping(value = "/springmvc" ,params={"username","age!=10"},headers={"Accept-language=zh-CN,zh;q=0.8"})
@Controller
public class HelloWorld {
@RequestMapping(value = "/springmvc")
public String hello() {
System.out.println("hello world");
return "success2";
}
}
- 结果1:
访问成功:http://localhost:9090/springmvc/springmvc?username=isj&age=11
访问失败:http://localhost:9090/springmvc/springmvc?username=isj&age=10因为age不能为10
结论:@RequestMapping 的 params 参数表示访问中必须包含某个参数
测试 @RequestMapping 的 headers
package com.atguigu.springmvc.handlers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//@RequestMapping(value = "/springmvc" ,params={"username","age!=10"})
@RequestMapping(value = "/springmvc" ,params={"username","age!=10"},headers={"Accept-language=zh-CN,zh;q=0.9,zh-TW;q=0.8"})
@Controller
public class HelloWorld {
@RequestMapping(value = "/springmvc")
public String hello() {
System.out.println("hello world");
return "success2";
}
}
对结果页面打开开发者工具(按F12)找到network标签下localhost文件选中,并刷新整个页面(为了得到headers),查看localhost可以得到request headers 如下
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,zh-TW;q=0.8
Cache-Control: no-cache
Connection: keep-alive
Cookie: JSESSIONID=DCB16D54094CF3DBFB7B22482B9F0077
DNT: 1
Host: localhost:9090
Pragma: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
开发者工具如下:
以 Accept-Language: zh-CN,zh;q=0.9,zh-TW;q=0.8 为例对HelloWorld.java修改@requestMapping()的headers参数。
package com.atguigu.springmvc.handlers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping(value = "/springmvc" ,params={"username","age!=10"},headers={"Accept-language=zh-CN,zh;q=0.9,zh-TW;q=0.8"})
@Controller
public class HelloWorld {
@RequestMapping(value = "/springmvc")
public String hello() {
System.out.println("hello world");
return "success2";
}
}
访问成功:
http://localhost:9090/springmvc/springmvc?username=isj&age=11
访问失败:当 @RequestMapping(value = "/springmvc" ,params={"username","age!=10"},headers={"Accept-language=en-US,zh;q=0.9,zh-TW;q=0.8"}) 则无法访问http://localhost:9090/springmvc/springmvc?username=isj&age=11
结论: @RequestMapping 的 headers 表示请求头包含某个参数必须与浏览器对应才能访问。
@RequestMapping() 的 value 支持 ant 风格三种匹配符:
?:匹配文件名中的一个字符
*:匹配文件名中的任意多个字符
**:** 匹配多层路径
例如:下面的地址可以为 testAntPath/a/abc 或者 testAntPath/abc/abc
@RequestMapping("/testAntPath/*/abc")
public String testAntPath(){
System.out.println("testAntPath")
}
Reference:感谢佟刚大神的SpringMVC视频教程