一:REST
- REST:即 Representational State Transfer。(资源)表现层状态转化,是目前最流行的一种互联网软件架构,它结构清晰,符合标准,易于理解,扩展方便。所以正得到越来越多的网站使用。
- 资源(Resources):网络上的一个实体,或者说网络上的一个具体信息,它可以是一个文本,一个图片,一个歌曲,一个服务,总之就是一个具体的存在,可以用一个URI(统一资源定位符)指向它,每种资源对应一个特定的URI。要获取这个资源,访问这个URI就可以,因此URI为每个资源独一无二的识别符。
- 表现层(Representational):把资源具体呈现出来的形式,叫他的表现层,比如,文本可以用txt格式表现,也可以用HTML格式,XML格式,JSON格式,甚至是二进制格式的。
- 状态转化(state Transf):每发出一个请求,就代表了客户端和服务器一次交互过程。HTTP协议,是一个无状态的协议,即所以的状态都保存在服务器端。因此,如果客户端想要操作服务器,必须通过某种手段,让服务器发生状态变化(state transf)。而这种转化是建立在表现层之上的,所以就是"表现层转化",具体说,就是HTTP协议里面,四个表示操作方式的动词,GET,POST,PUT,DELETE,他们分别对应四种基本操作,GET用来获取资源,POST用来新建资源,PUT用来更新资源,DELETE用来删除资源。
- HiddenHttpMethodFilter:浏览器Form表单只支持GET和POST请求,而PUT和DELETE等method方法不支持,Spring3.0添加了一个过滤器,可以将这些请求转化为标准的http方法,使得支持GET,POST,PUT,DELETE的请求。
二:代码实现
1.在web.xml中配置HiddenHttpMethodFilter
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>springMVC-1</display-name>
<!-- 配置org.springframework.web.filter.HiddenHttpMethodFilter
作用:可以把POST请求转成PUT和DELETE请求 -->
<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>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置Spring MVC配置文件的名称和位置 -->
<!--
实际上也可以不通过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>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.配置控制器
package com.dhx.handler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import jdk.nashorn.internal.ir.RuntimeNode.Request;
@RequestMapping("spring")
@Controller
public class SpringTest {
private final String SUCCESS="success";
/**
* Rest 风格的 URL.
* 以 CRUD 为例:
* 新增: /order POST
* 修改:/order/1 PUT update?id=1
* 获取:/order/1 GET get?id=1
* 删除:/order/1 DELETE delete?id=1
*
* 如何发送 PUT 请求和 DELETE 请求呢 ?
* 1. 需要配置 HiddenHttpMethodFilter
* 2. 需要发送 POST 请求
* 3. 需要在发送 POST 请求时携带一个 name="_method" 的隐藏域, 值为 DELETE 或 PUT
*
* 在 SpringMVC 的目标方法中如何得到 id 呢? 使用 @PathVariable 注解
*
*/
@ResponseBody
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.PUT)
public String testRestPut(@PathVariable Integer id) {
System.out.println("testRest Put: " + id);
return SUCCESS;
}
@ResponseBody
@RequestMapping(value = "/testRest/{id}")
public String testRestDelete(@PathVariable Integer id) {
System.out.println("testRest Delete: " + id);
return SUCCESS;
}
@RequestMapping(value = "/testRest", method = RequestMethod.POST)
public String testRest() {
System.out.println("testRest POST");
return SUCCESS;
}
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.GET)
public String testRest(@PathVariable Integer id) {
System.out.println("testRest GET: " + id);
return SUCCESS;
}
}
3.编写视图
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="spring/testRest/1" method="post">
<input type="hidden" name="_method" value="PUT"/>
<input type="submit" value="TestRest PUT"/>
</form>
<br><br>
<form action="spring/testRest/1" method="post">
<input type="hidden" name="_method" value="DELETE"/>
<input type="submit" value="TestRest DELETE"/>
</form>
<br><br>
<form action="spring/testRest" method="post">
<input type="submit" value="TestRest POST"/>
</form>
<br><br>
<a href="spring/testRest/1">Test Rest Get</a>
<br><br>
</body>
</html>
三:出现问题
1.有时候POST请求转成PUT或DELETE请求时会报错
Type Status Report
Message JSPs only permit GET POST or HEAD
Description The method received in the request-line is known by the origin server but not supported by the target resource.
2.解决办法只需要加上@ResponseBody注解
@ResponseBody
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.PUT)
public String testRestPut(@PathVariable Integer id) {
System.out.println("testRest Put: " + id);
return SUCCESS;
}