SpringMVC DELETE,PUT請求報錯 添加支援Http的DELETE、PUT請求

來源:互聯網
上載者:User

標籤:

SpringMVC刪除與修改操作需要用DELETE,PUT請求方式提交。

但要知道瀏覽器form表單只支援GET與POST請求,而DELETE、PUT等method並不支援。

spring3.0添加了一個過濾器,可以將這些請求轉換為標準的http方法,使得支援GET、POST、PUT與DELETE請求。

這個過濾器需要在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>springmvc</servlet-name>  
</filter-mapping> 
 注:其中springmvc是DispatcherServlet的servlet-name的名稱,配成其他如/*等之類的頁面請求會報錯。

頁面提交方式需轉換成post提交方式(可寫jquery方式提交表單)。

需要在頁面上添加隱藏欄位告訴controller此請求是哪種請求方式:

<input type="hidden" name="_method" value="DELETE"/>

<input type="hidden" name="_method" value="PUT"/>

這樣,就可實現刪除與更新的操作了。

SpringMVC DELETE,PUT請求報錯 添加支援Http的DELETE、PUT請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.