本文主要是介绍SpringMVC_004_HiddenHttpMethodFilter,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
html中form表单只支持GET与POST请求,而DELETE、PUT等method并不支持,spring3添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求。
在web.xml中加入
<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>
示例如下:
package com.ack.handler;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;@Controller
@RequestMapping("hhmft")
public class HiddenHttpMethodFilterTest {private static final String SUCCESS = "success";@RequestMapping(value="delete", method=RequestMethod.DELETE)public String restDelete(){System.out.println("-->>HiddenHttpMethodFilterTest/restDelete方法");return SUCCESS;}@RequestMapping(value="put", method=RequestMethod.PUT)public String restPut(){System.out.println("-->>HiddenHttpMethodFilterTest/restPut方法");return SUCCESS;}
}
在form中加<input type="hidden" name="_method" value="put" />支持PUT
在form中加<input type="hidden" name="_method" value="delete" />支持DELETE
hhmft/delete和hhmft/put访问。
PUT和DELETE请求报错,HTTP Status 405 - JSPs only permit GET POST or HEAD;
The JSP 2.3 specification requires JSPs to respond to GET, HEAD and POST only. The behaviour for all other HTTP methods is undefined. Tomcat opted to reject them to protect against HTTP verb tampering attacks. Since this is JSP 2.3, the change applies to Tomcat 8 onwards;
大意为:JSP2.3版本特别要求JSP页面只对GET,POST和HEAD请求进行应答;而对其它HTTP请求不予应答。为防止HTTP verB tampering攻击,Tomcat选择拒绝请求;Tomcat从8.0版本起遵循JSP2.3的这一要求;(所以才有了Tomcat7可以正常跳转页面,而Tomcat8不能);
这篇关于SpringMVC_004_HiddenHttpMethodFilter的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!