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>
<servlet-name>DispatcherServlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>DispatcherServlet</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>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
页面配置:
<form action="/rest/rest/100" method="post">
<input type="hidden" name="_method" value="put">
<input type="submit">
</form>
测试代码:
package com.xysf.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
//@Controller
//@ResponseBody
@org.springframework.web.bind.annotation.RestController
@RequestMapping("rest")
public class RestController {
@RequestMapping("rest1")
public String rest1(){
return "save.jsp";
}
@RequestMapping("{id}")
public String rest1(@PathVariable Integer id){
System.out.println("变量:"+id);
return "save.jsp";
}
@PutMapping("{id}")
public String rest2(@PathVariable Integer id){
System.out.println("变量:"+id);
System.out.println("put");
return "save.jsp";
}
}