•Spring MVC 使用 @RequestMapping 注解为控制器指定可 以处理哪些 URL 请求
•在控制器的类定义及方法定义处都可标注
@RequestMapping
–类定义处:提供初步的请求映射信息。相对于 WEB 应用的根目录
–方法处:提供进一步的细分映射信息。相对于类定义处的 URL。若
类定义处未标注 @RequestMapping,则方法处标记的 URL 相对于
WEB 应用的根目录
•DispatcherServlet 截获请求后,就通过控制器上
@RequestMapping 提供的映射信息确定请求所对应的处理 方法。
使用 @RequestMapping 映射请求示例
类定义处标记的
@RequestMapping 限定了处理 器类可以处理所有 URI 为 /hello 的请求,它相对于 WEB 容器部 署的根路径
处理器类可以定义多个处理方法,处理来 自/hello 下的请求
映射请求参数、请求方法或请求头
•@RequestMapping 除了可以使用请求 URL 映射请求外,
还可以使用请求方法、请求参数及请求头映射请求
•@RequestMapping 的 value 、 method 、 params 及 heads 分别表示请求 URL、请求方法、请求参数及请求头的映射条 件,他们之间是与的关系,联合使用多个条件可让请求映射 更加精确化。
•params 和 headers支持简单的表达式:
–param1: 表示请求必须包含名为 param1 的请求参数
–!param1: 表示请求不能包含名为 param1 的请求参数
–param1 != value1: 表示请求包含名为 param1 的请求参数,但其值 不能为 value1
–{“param1=value1”, “param2”}: 请求必须包含名为 param1 和param2 的两个请求参数,且 param1 参数的值必须为 value1
映射请求参数、请求方法或请求头
•Ant 风格资源地址支持 3 种匹配符:
–?:匹配文件名中的一个字符
–*:匹配文件名中的任意字符
–**:** 匹配多层路径
•@RequestMapping 还支持 Ant 风格的 URL:
–/user/*/createUser: 匹配
/user/aaa/createUser、/user/bbb/createUser 等 URL
–/user/**/createUser: 匹配
/user/createUser、/user/aaa/bbb/createUser 等 URL
–/user/createUser??: 匹配
/user/createUseraa、/user/createUserbb 等 URL
P707. RequestMapping_PathVariable注解
@PathVariable 映射 URL 绑定的占位符
•带占位符的 URL 是 Spring3.0 新增的功能,该功能在
SpringMVC 向 REST 目标挺进发展过程中具有里程碑的 意义
•通过 @PathVariable 可以将 URL 中占位符参数绑定到控 制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过
@PathVariable("xxx") 绑定到操作方法的入参中。
REST
•REST:即 Representational State Transfer。(资源)表现层状态转化。是目前 最流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便, 所以正得到越来越多网站的采用
•资源(Resources):网络上的一个实体,或者说是网络上的一个具体信息。它 可以是一段文本、一张图片、一首歌曲、一种服务,总之就是一个具体的存在。 可以用一个URI(统一资源定位符)指向它,每种资源对应一个特定的 URI 。要 获取这个资源,访问它的URI就可以,因此 URI 即为每一个资源的独一无二的识 别符。
•表现层(Representation):把资源具体呈现出来的形式,叫做它的表现层
(Representation)。比如,文本可以用 txt 格式表现,也可以用 HTML 格 式、XML 格式、JSON 格式表现,甚至可以采用二进制格式。
•状态转化(State Transfer):每发出一个请求,就代表了客户端和服务器的一 次交互过程。HTTP协议,是一个无状态协议,即所有的状态都保存在服务器 端。因此,如果客户端想要操作服务器,必须通过某种手段,让服务器端发生“
状态转化”(State Transfer)。而这种转化是建立在表现层之上的,所以就是 “
表现层状态转化”。具体说,就是 HTTP 协议里面,四个表示操作方式的动
词:GET、POST、PUT、DELETE。它们分别对应四种基本操作:GET 用来获 取资源,POST 用来新建资源,PUT 用来更新资源,DELETE 用来删除资源。
•示例:
–/order/1 HTTP GET :得到 id = 1 的 order
–/order/1 HTTP DELETE:删除 id = 1的 order
–/order/1 HTTP PUT:更新id = 1的 order
–/order HTTP POST:新增 order
•HiddenHttpMethodFilter:浏览器 form 表单只支持 GET
与 POST 请求,而DELETE、PUT 等 method 并不支
持,Spring3.0 添加了一个过滤器,可以将这些请求转换 为标准的 http 方法,使得支持 GET、POST、PUT 与
DELETE 请求。
@PathVariable 绑定 URL 占位符到入参
•带占位符的 URL 是 Spring3.0 新增的功能,该功能在
SpringMVC 向 REST 目标挺进发展过程中具有里程碑的 意义
•通过 @PathVariable 可以将 URL 中占位符参数绑定到控 制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过 @PathVariable("xxx") 绑定到操作方法的入参中。
代码示例:
helloWorld.java
package com.xuehj.springmvc.handler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @program: SpringMVC
* @description: TODO
* @author: Mr.Xue
* @create: 2018-12-24 09:36
**/
@Controller
@RequestMapping("/view")
public class HelloWorld {
/**
* 1. 使用 @RequestMapping 注解来映射请求的 URL
* 2. 返回值会通过视图解析器解析为实际的物理视图, 对于 InternalResourceViewResolver 视图解析器,
* 会做如下的解析:通过 prefix + returnVal + 后缀 这样的方式得到实际的物理视图, 然会做转发操作
* /WEB-INF/views/success.jsp
*
* @return
*/
@RequestMapping("/helloworld")
public String hello() {
System.out.println("hello world");
return "success.jsp";
}
/**
* 常用: 使用 method 属性来指定请求方式
*/
@RequestMapping(value = "/testMethod", method = RequestMethod.POST)
public String testMethod() {
System.out.println("testMethod");
return "success.jsp";
}
/**
* 了解: 可以使用 params 和 headers 来更加精确的映射请求. params 和 headers 支持简单的表达式.
*
* @return
*/
@RequestMapping(value = "testParamsAndHeaders", params = {"username",
"age!=10"}, headers = {"Accept-Language=en-US,zh;q=0.8"})
public String testParamsAndHeaders() {
System.out.println("testParamsAndHeaders");
return "success.jsp";
}
@RequestMapping("/testAntPath/*/abc")
public String testAntPath() {
System.out.println("testAntPath");
return "success.jsp";
}
/**
* @param id
* @return
* @PathVariable 可以来映射 URL 中的占位符到目标方法的参数中.
*/
@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") int id) {
System.out.println("testPathVariable: " + id);
return "../success.jsp";
}
/**
* Rest 风格的 URL.
* 以 CRUD 为例:
* 新增: /order POST
* 修改: /order/1 PUT update?id=1
* 获取: /order/1 GET get?id=1
* 删除: /order/1 DELETE delete?id=1
* <p>
* 如何发送 PUT 请求和 DELETE 请求呢 ?
* 1. 需要配置 HiddenHttpMethodFilter
* 2. 需要发送 POST 请求
* 3. 需要在发送 POST 请求时携带一个 name="_method" 的隐藏域, 值为 DELETE 或 PUT
* <p>
* 在 SpringMVC 的目标方法中如何得到 id 呢?
* 使用 @PathVariable 注解
*/
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.GET)
public String testRest(@PathVariable Integer id) {
System.out.println("testRest GET: " + id);
return "../success.jsp";
}
@RequestMapping(value = "/testRest", method = RequestMethod.POST)
public String testRest() {
System.out.println("testRest POST");
return "success.jsp";
}
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.DELETE)
public String testRestDelete(@PathVariable Integer id) {
System.out.println("testRest Delete: " + id);
return "../success.jsp";
}
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.PUT)
public String testRestPut(@PathVariable Integer id) {
System.out.println("testRest Put: " + id);
return "../success.jsp";
}
}
success.jsp
<%--
Created by IntelliJ IDEA.
User: 薛恒杰
Date: 2018/12/24
Time: 9:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
dispatcher-servlet.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.xuehj.springmvc"/>
<!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--<property name="prefix" value="/view/"/>-->
<!--<property name="suffix" value=".jsp"/>-->
<property name="prefix" value=""/>
<property name="suffix" value=""/>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELETE 或 PUT 请求
-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置 DispatcherServlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置 DispatcherServlet 的一个初始化参数: 配置 SpringMVC 配置文件的位置和名称 -->
<!--
实际上也可以不通过 contextConfigLocation 来配置 SpringMVC 的配置文件, 而使用默认的.
默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml
-->
<!--
<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>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<%--
Created by IntelliJ IDEA.
User: 薛恒杰
Date: 2018/12/24
Time: 9:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="view/helloworld">Hello world</a>
<br><br>
<a href="view/testMethod">testMethod</a>
<br><br>
<form method="post" action="${pageContext.request.contextPath}/view/testMethod">
<input type="submit" value="提交"/>
</form>
<br><br>
<a href="view/testParamsAndHeaders">Test ParamsAndHeaders</a>
<br><br>
<a href="view/testAntPath/ddd/abc">Test AntPath</a>
<br><br>
<a href="view/testPathVariable/1">Test PathVariable</a>
<br><br>
<a href="view/testRest/1">Test Rest Get</a>
<br><br>
<form action="view/testRest" method="post">
<input type="submit" value="TestRest POST"/>
</form>
<br><br>
<form action="view/testRest/1" method="post">
<input type="hidden" name="_method" value="DELETE"/>
<input type="submit" value="TestRest DELETE"/>
</form>
<br><br>
<form action="view/testRest/1" method="post">
<input type="hidden" name="_method" value="PUT"/>
<input type="submit" value="TestRest PUT"/>
</form>
</body>
</html>