瀏覽器form表單只支持GET與POST請求,而DELETE、PUT等method并不支持,spring3.0添加了一個(gè)過濾器,可以將這些請求轉(zhuǎn)換為標(biāo)準(zhǔn)的http方法,使得支持GET、POST、PUT與DELETE請求。
1.配置springmvc配置文件springmvc-servlet.xml
<!-- 瀏覽器不支持put,delete等method,由該filter將/xxx?_method=delete轉(zhuǎn)換為標(biāo)準(zhǔn)的http delete方法 -->
<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>springmvc</servlet-name>
</filter-mapping>
其中springmvc是DispatcherServlet的名稱
2.在對應(yīng)的Controller中,添加對應(yīng)的請求注解
/** 進(jìn)入新增 */
@RequestMapping(value="/new")
/** 顯示 */
@RequestMapping(value="/{id}")
/** 編輯 */
@RequestMapping(value="/{id}/edit")
/** 保存新增 */
@RequestMapping(method=RequestMethod.POST)
/** 保存更新 */
@RequestMapping(value="/{id}",method=RequestMethod.PUT)
/** 刪除 */
@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
/** 批量刪除 */
@RequestMapping(method=RequestMethod.DELETE)
進(jìn)入新增頁面時(shí)沒有用add而是用new,是因?yàn)槟承g覽器會(huì)將add當(dāng)做廣告攔截掉。
3.頁面請求
<form:form action="/xxx/xxx" method="put">
</form:form>
生成的頁面代碼會(huì)添加一個(gè)hidden的_method=put,并于web.xml中的HiddenHttpMethodFilter配合使用,在服務(wù)端將post請求改為put請求
<form id="userInfo" action="/xxx/xxx" method="post">
<input type="hidden" name="_method" value="put"/>
</form>
另外也可以用ajax發(fā)送delete、put請求