概念
这里Restful风格指通过HTTP请求对应的POST,GET,PUT,DELETE方法来完成对应的CRUD操作。
如何做?
Controller
@ResponseBody
@RequestMapping(value="/oper/{str}", method = RequestMethod.GET)
public Object get(@PathVariable("str") String str ){
return "get";
}
@ResponseBody
@RequestMapping(value="/oper/{str}", method = RequestMethod.POST)
public Object post(@PathVariable("str") String str ){
return "post";
}
@ResponseBody
@RequestMapping(value="/oper/{str}", method = RequestMethod.PUT)
public Object update(@PathVariable("str") String str ){
return "update";
}
@ResponseBody
@RequestMapping(value="/oper/{str}", method = RequestMethod.DELETE)
public Object delete(@PathVariable("str") String str ){
return "delete";
}
web.xml配置:
<filter>
<filter-name>temp</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>temp</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
form表单:
POST:
<form method="post">
...
</form>
GET:
<form method="get">
...
</form>
PUT:
<form method="post">
...
<input type="hidden" name="_method" value="PUT"/>
</form>
DELETE:
<form method="post">
...
<input type="hidden" name="_method" value="DELETE"/>
</form>
简单解释
因为form表单的method属性只支持get/post,所以put/delete时,仍然通过post方法请求,不同的是需要添加_method参数,配置的HiddenHttpMethodFilter通过该参数来转换请求。