SpringMVC学习(4): HiddenHttpMethodFilter
2017-04-22 00:16
239 查看
因为浏览器form表单只支持GET请求和POST请求,而不支持DELETE、PUT请求,因此在Spring3.0中添加了一个过滤器HiddenHttpMethodFilter,可以将这些请求转为标准的http方法,使得支持GET、POST、PUT和DELETE请求。这也使得其具备了REST风格。
在web.xml文件中配置HiddenHttpMethodFilter
<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>在java文件中:
package springmvc;
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;
@Controller
@RequestMapping("/springmvc")
public class HelloWorld {
private static final String SUCCESS = "success";
@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", method=RequestMethod.POST)
public String testRest() {
System.out.println("testRest POST");
return SUCCESS;
}
@RequestMapping(value = "/testRest/{id}", method=RequestMethod.GET)
public String testRest(@PathVariable("id") Integer id) {
System.out.println("testRest GET: " + id);
return SUCCESS;
}
}
然后在index.jsp文件中使用hidden域
<%@ 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>SpringMVC</title>
</head>
<body>
<form action="springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="TestRest PUT">
</form>
<br><br>
<form action="springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="TestRest DELETE">
</form>
<br><br>
<form action="springmvc/testRest" method="post">
<input type="submit" value="TestRest POST">
</form>
<br><br>
<a href="springmvc/testRest/1">Test Rest Get</a>
<br><br>
</body>
</html>运行一下可以看到运行结果正常。
在这里需要注意的一点是:Tomcat应该使用7.0版本的,因为使用8.0以上的版本,Tomcat会处于对JSP文件的保护,使得PUT和DELETE方法被拒绝,从而返回一个405的错误。
在web.xml文件中配置HiddenHttpMethodFilter
<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>在java文件中:
package springmvc;
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;
@Controller
@RequestMapping("/springmvc")
public class HelloWorld {
private static final String SUCCESS = "success";
@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", method=RequestMethod.POST)
public String testRest() {
System.out.println("testRest POST");
return SUCCESS;
}
@RequestMapping(value = "/testRest/{id}", method=RequestMethod.GET)
public String testRest(@PathVariable("id") Integer id) {
System.out.println("testRest GET: " + id);
return SUCCESS;
}
}
然后在index.jsp文件中使用hidden域
<%@ 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>SpringMVC</title>
</head>
<body>
<form action="springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="TestRest PUT">
</form>
<br><br>
<form action="springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="TestRest DELETE">
</form>
<br><br>
<form action="springmvc/testRest" method="post">
<input type="submit" value="TestRest POST">
</form>
<br><br>
<a href="springmvc/testRest/1">Test Rest Get</a>
<br><br>
</body>
</html>运行一下可以看到运行结果正常。
在这里需要注意的一点是:Tomcat应该使用7.0版本的,因为使用8.0以上的版本,Tomcat会处于对JSP文件的保护,使得PUT和DELETE方法被拒绝,从而返回一个405的错误。
相关文章推荐
- 8. SpringMVC_RequestMapping_HiddenHttpMethodFilter 过滤器
- SpringMVC-HiddenHttpMethodFilter
- 练习SpringMVC过滤器HiddenHttpMethodFilter遇到的坑
- Spring MVC 学习手记(3)- HiddenHttpMethodFilter 过滤器
- 【springMVC】RequestMapping_HiddenHttpMethodFilter 过滤器在tomcat7/8实现的问题
- SpringMVC(三)HiddenHttpMethodFilter
- SpringMVC -- @PathVariable/Rest风格的URL HiddenHttpMethodFilter (2)
- SpringMVC_004_HiddenHttpMethodFilter
- SpringMVC(四):@RequestMapping结合org.springframework.web.filter.HiddenHttpMethodFilter实现REST请求
- SpringMVC(五):HiddenHttpMethodFilter实现REST请求
- SpringMVC ------HiddenHttpMethodFilter 过滤器
- SpringMVC 过滤器之HiddenHttpMethodFilter
- SpringMVC 之 HiddenHttpMethodFilter 实现原理
- springMVC------Rest风格,HiddenHttpMethodFilter的作用
- SpringMVC 过滤器HiddenHttpMethodFilter
- 3.SpringMVC_HiddenHttpMethodFilter
- Spring MVC 使用HiddenHttpMethodFilter配置Rest风格的URL
- Spring MVC过滤器-HiddenHttpMethodFilter
- Spring MVC过滤器-HiddenHttpMethodFilter
- Spring MVC过滤器-HiddenHttpMethodFilter