SpringMVC_004_HiddenHttpMethodFilter

来源:互联网 发布:淘宝落地窗帘布 编辑:程序博客网 时间:2024/05/20 22:37

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不能);

If you redirect to an error page (rather than using the error handling mechanism) you'll need to set isErrorPage="true" and wait for 8.0.12 to be released.
如果你不希望使用错误处理机制,Tomcat8.0.12支持在JSP中设置iserrorpage =“true”后,跳转到错误页面;


0 0
原创粉丝点击