1.HTML端代碼
<h1>這個以下是測試REST功能區的代碼</h1>
<form action="/rest/doOrder" method="post">
<input type="submit" value="DO POST ADD" />
</form>
<br>
<form action="/rest/doOrder/10" method="post">
<input type="hidden" name="_method" value="PUT" />
<input type="submit" value="DO PUT" />
</form>
<br>
<form action="/rest/doOrder/11" method="post">
<input type="hidden" name="_method" value="DELETE" />
<input type="submit" value="DO DELETE" />
</form>
<br>
<a href="/rest/doOrder/12">測試GET功能</a>
2.Controller代碼
@Controller
@RequestMapping("/rest")
public class RESTDemoController {
private static final String SUCCESS = "success";
@RequestMapping(value = "/doOrder", method = RequestMethod.POST)
public String doAdd() {
System.out.println("this is do ADD ");
return SUCCESS;
}
@RequestMapping(value = "/doOrder/{id}", method = RequestMethod.PUT)
public String doPut(@PathVariable Integer id) {
System.out.println("this is do doPut " + id);
return SUCCESS;
}
@RequestMapping(value = "/doOrder/{id}", method = RequestMethod.DELETE)
public String doDelete(@PathVariable Integer id) {
System.out.println("this is do Delete " + id);
return SUCCESS;
}
@RequestMapping(value = "/doOrder/{id}", method = RequestMethod.GET)
public String doGet(@PathVariable Integer id) {
System.out.println("this is do Get " + id);
return SUCCESS;
}
}
3.web.xml代碼
<!--
org.springframework.web.filter.HiddenHttpMethodFilter
這個是一個將POST請求轉化爲DELETE或POST請求
-->
<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>
這就ok了