REST:表现层状态转化。
- REST 是目前最流行的一种互联网软件架构。他结构清晰。符合标准、易于理解、扩展方便 , 所以正得到越来越多网站的采用。
- 状态转化:浏览器 form 表单只支持 GET 和 POST 请求 , 而 DELETE 、PUT 等 method 名不支持 , Spring3.0 添加 HiddenHttpMethodFilter 过滤器 , 可以将这些请求转换为标准的 http 方法 , 使得支持 GET , POST , PUT 与 DELETE 请求。
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 version="3.1"> 6 7 <!-- 8 配置 org.springframework.web.filter.HiddenHttpMethodFilter 拦截器, 9 可以把 POST 请求转化为 PUT , DELETE 请求。 10 --> 11 <filter> 12 <filter-name>hiddenHttpMethodFilter</filter-name> 13 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 14 </filter> 15 <filter-mapping> 16 <filter-name>hiddenHttpMethodFilter</filter-name> 17 <url-pattern>/*</url-pattern> 18 </filter-mapping> 19 <context-param> 20 <param-name>contextConfigLocation</param-name> 21 <param-value>/WEB-INF/applicationContext.xml</param-value> 22 </context-param> 23 <listener> 24 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 25 </listener> 26 <servlet> 27 <servlet-name>dispatcher</servlet-name> 28 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 29 <load-on-startup>1</load-on-startup> 30 </servlet> 31 <servlet-mapping> 32 <servlet-name>dispatcher</servlet-name> 33 <url-pattern>/</url-pattern> 34 </servlet-mapping> 35 </web-app>
dispatcher-servlet.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 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"> 6 <!-- 扫描自定义包 --> 7 <context:component-scan base-package="com.itdoc.springmvc"/> 8 9 <!-- 配置试图解析器: 把 Controller 返回值解析成实际的物理视图 --> 10 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 11 <property name="prefix" value="/WEB-INF/views/"/> 12 <property name="suffix" value=".jsp"/> 13 </bean> 14 </beans>
TestRest.java
1 package com.itdoc.springmvc; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.PathVariable; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestMethod; 7 8 /** 9 * @BLOG http://www.cnblogs.com/goodcheap 10 * @DESCRIBE REST 风格 11 * @AUTHOR WángChéngDá 12 * @DATE 2017-03-09 8:58 13 */ 14 @Controller 15 public class TestRest { 16 17 private final static String SUCCESS = "success"; 18 19 20 /** 21 * REST 风格的 URL 22 * 1.配置 HiddenHttpMethodFilter。 23 * 2.发送 POST 请求。发送 POST 请求同时必须携带一个 name="_method" 的隐藏域, 24 * 值为 DELETE 或 PUT, 不区分大小写。 25 * 3.通过 HiddenHttpMethodFilter 将 POST 请求转换为想要转换的 DELETE 或 PUT 请求。 26 * 4.通过 @PathVariable 注解接收请求中占位符 {xxx} 中的值到方法的入参中。 27 */ 28 @RequestMapping(value = "/testDelete/{id}", method = RequestMethod.DELETE) 29 public String testDelete(@PathVariable("id") Integer id) { 30 System.out.println("I am TestRest's testDelete method... id: " + id); 31 return SUCCESS; 32 } 33 34 @RequestMapping(value = "/testPut/{id}", method = RequestMethod.PUT) 35 public String testPut(@PathVariable("id") Integer id) { 36 System.out.println("I am TestRest's testPut method... id: " + id); 37 return SUCCESS; 38 } 39 40 @RequestMapping(value = "/testPost", method = RequestMethod.POST) 41 public String testPost() { 42 System.out.println("I am TestRest's testPost method..."); 43 return SUCCESS; 44 } 45 46 @RequestMapping("/testGet") 47 public String testGet() { 48 System.out.println("I am TestRest's testGet method..."); 49 return SUCCESS; 50 } 51 }