Spring MVC HiddenHttpMethodFilter 实现 REST风格的URL
2016-11-07 13:12
387 查看
同样接着上一篇的来,我们首先去web.xml中配置HiddenHttpMethodFilter
web.xml
在index.jsp中加上以下的超链接测试用。
这样就能实现DELETE、PUT、POST、GET请求。
重复啰嗦一下。
如何发送 PUT 请求和 DELETE 请求呢?
* 1. 需要配置 HiddenHttpMethodFilter
* 2. 需要发送POST请求
* 3. 需要在发送POST请求时携带一个 name=”_method” 的隐藏域, 值为 DELETE 或 PUT
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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>springmvc1</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置org.springframework.web.filter.HiddenHttpMethodFilter:可以把POST请求转为DELETE或POST请求 --> <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>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置DispatcherServlet的一个初始化参数:配置SprngMVC配置文件的位置和名称 --> <!-- 实际上也可以不通过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>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
在index.jsp中加上以下的超链接测试用。
<form action="springmvc/testRest/1" method="post"> <input type="hidden" name="_method" value="PUT"> <input type="submit" value="TestRest PUT"> </form> <br /> <form action="springmvc/testRest/1" method="post"> <input type="hidden" name="_method" value="DELETE"> <input type="submit" value="TestRest DELETE"> </form> <br /> <form action="springmvc/testRest/1" method="post"> <input type="submit" value="TestRest POST"> </form> <br /> <a href="springmvc/testRest/1">Test Rest Get</a> <br />
package com.hust.springmvc1; 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; import org.springframework.web.bind.annotation.SessionAttributes; @SessionAttributes(value={"user"}, types={String.class}) @Controller @RequestMapping("/springmvc") public class SpringMVCTest { private static 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 注解 */ @RequestMapping(value = "/testRest/{id}", method = RequestMethod.PUT) public String testRestPut(@PathVariable Integer id) { System.out.println("testRest Put: " + id); return SUCCESS; } @RequestMapping(value = "/testRest/{id}", method = RequestMethod.DELETE) public String testRestDelete(@PathVariable Integer id) { System.out.println("testRest Delete: " + id); return SUCCESS; } @RequestMapping(value = "/testRest/{id}", 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; } }
这样就能实现DELETE、PUT、POST、GET请求。
重复啰嗦一下。
如何发送 PUT 请求和 DELETE 请求呢?
* 1. 需要配置 HiddenHttpMethodFilter
* 2. 需要发送POST请求
* 3. 需要在发送POST请求时携带一个 name=”_method” 的隐藏域, 值为 DELETE 或 PUT
相关文章推荐
- SpringMVC -- @PathVariable/Rest风格的URL HiddenHttpMethodFilter (2)
- Spring MVC 使用HiddenHttpMethodFilter配置Rest风格的URL
- Spring MVC 使用HiddenHttpMethodFilter配置Rest风格的URL
- Spring MVC 使用拦截器 HiddenHttpMethodFilter配置Rest风格的URL
- SpringMVC(四):@RequestMapping结合org.springframework.web.filter.HiddenHttpMethodFilter实现REST请求
- SpringMVC HiddenHttpMethodFilter的使用
- springMVC------Rest风格,HiddenHttpMethodFilter的作用
- springmvc HiddenHttpMethodFilter
- SpringMVC(五):HiddenHttpMethodFilter实现REST请求
- SpringMVC 之 HiddenHttpMethodFilter 实现原理
- spring HiddenHttpMethodFilter使用问题
- spring的HiddenHttpMethodFilter
- 【springMVC】RequestMapping_HiddenHttpMethodFilter 过滤器在tomcat7/8实现的问题
- Spring3.0之后->Spring MVC过滤器-HiddenHttpMethodFilter
- SpringMVC用HiddenHttpMethodFilter配置Rest风格的URL
- org.springframework.web.filter.HiddenHttpMethodFilter
- Spring-web源码解析之Filter-HiddenHttpMethodFilter
- asp.net mvc实现rest风格返回json
- Spring MVC How take the parameter value of a GET HTTP Request in my controller method?
- Spring MVC过滤器-HiddenHttpMethodFilter