/**
* SpringMVC REST风格url测试
* 1、首先需要在web.xml中配置一个filter:org.springframework.web.filter.HiddenHttpMethodFilter
* 2、在请求form表单中使用隐藏域:hidden设置 _method 得值为 DELETE/PUT (这个可以在spring源码追溯到)
* 3、额外知识:使用tomcat8 请求delete和put时会报如下错误:
* HTTP Status 405 - JSPs only permit GET POST or HEAD.。
* 用tomcat7则没有错
*
* @return
*/
测试类:
package com.spring.controller;
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.ResponseBody;
@Controller
public class TestController {
/**
* SpringMVC REST风格url测试
* 1、首先需要在web.xml中配置一个filter:org.springframework.web.filter.HiddenHttpMethodFilter
* 2、在请求form表单中使用隐藏域:hidden设置 _method 得值为 DELETE/PUT (这个可以在spring源码追溯到)
* 3、额外知识:使用tomcat8 请求delete和put时会报如下错误:
* HTTP Status 405 - JSPs only permit GET POST or HEAD.。
* 用tomcat7则没有错
*
* @return
*/
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping(value="testRest/{id}",method=RequestMethod.DELETE)
@ResponseBody()
public String testRestDelete(@PathVariable(value="id") int id){
System.out.println("delete id == "+id);
return "success";
}
@RequestMapping(value="testRest/{id}",method=RequestMethod.PUT)
public String testRestPut(@PathVariable(value="id") int id){
System.out.println("put id == "+id);
return "success";
}
@RequestMapping(value="testRest",method=RequestMethod.POST)
public String testRestPost(){
System.out.println("POST");
return "success";
}
@RequestMapping(value="testRest/{id}",method=RequestMethod.GET)
public String testRest(@PathVariable(value="id") int id){
System.out.println("get id == "+id);
return "success";
}
}
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:component-scan base-package="com.spring"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
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>25SpringMVC02</display-name>
<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>
<!-- The front controller of this Spring Web application, responsible for
handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
web-inf/views/index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="testRest/1" method="get">
<input type="submit" value="GET请求">
</form>
<br>
<form action="testRest" method="post">
<input type="submit" value="POST请求">
</form>
<br>
<form action="testRest/2" method="post">
<input type="hidden" value="put" name="_method">
<input type="submit" value="PUT请求">
</form>
<br>
<form action="testRest/3" method="post">
<input type="hidden" value="delete" name="_method">
<input type="submit" value="DELETE请求">
</form>
<br>
</body>
</html>
success.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello Spring!
</body>
</html>