當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
Spring MVC 使用拦截器 HiddenHttpMethodFilter配置Rest风格的URL
生活随笔
收集整理的這篇文章主要介紹了
Spring MVC 使用拦截器 HiddenHttpMethodFilter配置Rest风格的URL
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<!-- 4、使用Rest風(fēng)格的URI,將頁(yè)面普通的post請(qǐng)求轉(zhuǎn)為指定的delete或者put請(qǐng)求 -->
詳細(xì)使用請(qǐng)參考這篇博客:地址:http://blog.csdn.net/pplcheer/article/details/74999748
Rest 風(fēng)格的 URL. 以 CRUD 為例: 新增: /order POST 修改: /order/1 PUT update?id=1 獲取:/order/1 GET get?id=1 刪除: /order/1 DELETE delete?id=1如何發(fā)送 PUT 請(qǐng)求和 DELETE 請(qǐng)求呢 ?
1. 需要在web.xml文件中配置 HiddenHttpMethodFilter <!-- 配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 請(qǐng)求轉(zhuǎn)為 DELETE 或 POST 請(qǐng)求 --><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><filter><filter-name>HttpPutFormContentFilter</filter-name><filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class></filter><filter-mapping><filter-name>HttpPutFormContentFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping> 補(bǔ)充說(shuō)明:
如果在web.xml中配置HttpPutFormContentFilter,配置如下
<filter> <filter-name>httpPutFormcontentFilter</filter-name> <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class> </filter> <filter-mapping> <filter-name>httpPutFormContentFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
?需要注意的是,該過(guò)濾器只能接受enctype值為application/x-www-form-urlencoded的表單,也就是說(shuō),在使用該過(guò)濾器時(shí),form表單的代碼必須如下:
?
<form action="" method="put" enctype="application/x-www-form-urlencoded"> ...... </form> 2. 需要發(fā)送 POST 請(qǐng)求3. 需要在發(fā)送 POST 請(qǐng)求時(shí)攜帶一個(gè) name="_method" 的隱藏域, 值為 DELETE 或 PUTjsp文件如下:<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>在 SpringMVC 的目標(biāo)方法中如何得到 id 呢? 使用 @PathVariable 注解*/@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 Integer id) {System.out.println("testRest GET: " + id);return SUCCESS;}/*** @PathVariable 可以來(lái)映射 URL 中的占位符到目標(biāo)方法的參數(shù)中.* @param id* @return*/@RequestMapping("/testPathVariable/{id}")public String testPathVariable(@PathVariable("id") Integer id) {System.out.println("testPathVariable: " + id);return SUCCESS;}@RequestMapping("/testAntPath/*/abc")public String testAntPath() {System.out.println("testAntPath");return SUCCESS;}轉(zhuǎn)載于:https://www.cnblogs.com/a8457013/p/8260833.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專(zhuān)家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的Spring MVC 使用拦截器 HiddenHttpMethodFilter配置Rest风格的URL的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: zabbix items 配置
- 下一篇: [k8s]elk架构设计-k8s集群里搭