2012-06-21 176 views
16

即時得到這個錯誤:HTTP Status 405 - Request method 'POST' not supportedHTTP狀態405 - 請求方法「POST」(不支持的Spring MVC)

我所試圖做的就是一個形式下拉框的下降是那些獲得基於其他填充在另一個下拉框中選擇的值。例如,當我在customerName框中選擇一個名稱時,應該運行.jsp頁面中的onChange函數,然後提交的頁面再次使用customerCountry框中的相應值加載。

但是,我收到此HTTP狀態405錯誤。我已經在互聯網上搜索了一個解決方案,但一直沒有找到任何幫助。這裏是我的代碼的相關部分:jsp頁面的

部分控制器

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Insert title here</title> 
      <style> 
      .error { color: red; } 
      </style> 

     <script> 
      function repopulate(){ 
       document.deliveryForm.submit(); 
      } 

      function setFalse(){ 
       document.getElementById("hasId").value ="false"; 
       document.deliveryForm.submit(); 
       // document.submitForm.submit(); (This was causing the error) 

      } 
     </script> 

    </head> 
    <body> 

     <h1>Create New Delivery</h1> 

     <c:url var="saveUrl" value="/test/delivery/add" /> 
     <form:form modelAttribute="deliveryDtoAttribute" method="POST" action="${saveUrl}" name="deliveryForm"> 
      <table> 


       <tr> 
        <td><form:hidden id="hasId" path="hasCustomerName" value="true"/></td> 
       </tr> 

       <tr> 
        <td>Customer Name</td> 
        <td><form:select path="customerName" onChange="repopulate()"> 
         <form:option value="" label="--- Select ---" /> 
         <form:options items="${customerNameList}" /> 
         </form:select> 
        </td> 
        <td><form:errors path="customerName" cssClass="error" /></td> 
       </tr> 

       <tr> 
        <td>Customer Country</td> 
        <td><form:select path="customerCountry"> 
         <form:option value="" label="--- Select ---" /> 
         <form:options items="${customerCountryList}" /> 
         </form:select> 
        </td> 
        <td><form:errors path="customerCountry" cssClass="error" /></td> 
       </tr> 

     </form:form> 

     <form:form name="submitForm"> 
     <input type="button" value="Save" onClick="setFalse()"/> 
     </form:form> 

    </body> 
</html> 

部分:

@RequestMapping(value = "/add", method = RequestMethod.GET) 
    public String getDelivery(ModelMap model) { 
     DeliveryDto deliveryDto = new DeliveryDto(); 

     model.addAttribute("deliveryDtoAttribute", deliveryDto); 
     model.addAttribute("customerNameList", 
       customerService.listAllCustomerNames()); 
     model.addAttribute("customerCountryList", customerService 
        .listAllCustomerCountries(deliveryDto.getCustomerName())); 
     return "new-delivery"; 
    } 

    // I want to enter this method if hasId=true which means that a value in the CustomerName 
    // drop down list was selected. This should set the CountryList to the corresponding values 
    // from the database. I want this post method to be triggered by the onChange in the jsp page 

    @RequestMapping(value = "/add", method = RequestMethod.POST, params="hasCustomerName=true") 
    public String postDelivery(
      @ModelAttribute("deliveryDtoAttribute") DeliveryDto deliveryDto, 
      BindingResult result, ModelMap model) { 


      model.addAttribute("deliveryDtoAttribute", deliveryDto); 

      model.addAttribute("customerNameList", 
        customerService.listAllCustomerNames()); 
      model.addAttribute("customerCountryList", customerService 
        .listAllCustomerCountries(deliveryDto.getCustomerName())); 

      return "new-delivery"; 
    } 

    // This next post method should only be entered if the save button is hit in the jsp page 

    @RequestMapping(value = "/add", method = RequestMethod.POST, params="hasCustomerName=false") 
    public String postDelivery2(
      @ModelAttribute("deliveryDtoAttribute") @Valid DeliveryDto deliveryDto, 
      BindingResult result, ModelMap model) { 

     if (result.hasErrors()) { 

      model.addAttribute("deliveryDtoAttribute", deliveryDto); 

      model.addAttribute("customerNameList", 
        customerService.listAllCustomerNames()); 
      model.addAttribute("customerCountryList", customerService 
        .listAllCustomerCountries(deliveryDto.getCustomerName())); 

      return "new-delivery"; 
     } else { 

      Delivery delivery = new Delivery(); 

      //Setters to set delivery values 

      return "redirect:/mis/home"; 
     } 

    } 

爲什麼我得到這個錯誤?任何幫助將非常感激!謝謝

編輯:更改hasIdhasCustomerName。儘管如此,我仍然得到了HTTP Status 405 - Request method 'POST' not supported錯誤。

EDIT2:註釋掉在setFalse功能是造成錯誤

// d

+1

爲什麼不合理?請給我一個解釋,以便我將來可以發佈更好的問題。感謝 – dlinx90

+0

對於遇到此問題並且出現在此頁面上的用戶,請檢查定義的操作是否碰到了現有的端點,這是我的問題:) –

回答

3

我發現,是造成HTTP錯誤的問題。

在由Save按鈕觸發的setFalse()函數中,我的代碼試圖提交包含該按鈕的表單。

 function setFalse(){ 
      document.getElementById("hasId").value ="false"; 
      document.deliveryForm.submit(); 
      document.submitForm.submit(); 

當我刪除document.submitForm.submit();它的工作原理:

 function setFalse(){ 
      document.getElementById("hasId").value ="false"; 
      document.deliveryForm.submit() 

@RogerLindsjö謝謝你察覺我的錯誤,我沒有傳遞正確的參數在哪裏!

3

的問題是,你的控制器所期望的線路參數hasId =假hasId =真正的,但你沒有通過。您的隱藏字段的編號爲hasId,但作爲hasCustomerName傳遞,因此沒有映射匹配。

無論是隱藏字段的路徑改變爲hasId或映射參數期望hasCustomerName =真hasCustomerName =假

+0

我寫這篇文章時看到我有點疲倦。好眼睛!儘管如此,我仍然得到完全相同的錯誤。任何想法它可能是什麼? – dlinx90

+1

錯誤信息如何誤導?它使我們認爲它是一個HTTP動詞問題,實際上它是一個參數。 – Stephane

2

您可能需要行

@RequestMapping(value = "/add", method = RequestMethod.GET) 

改變

@RequestMapping(value = "/add", method = {RequestMethod.GET,RequestMethod.POST}) 
+1

不,有兩種方法,一種用於每個RequestMethod。 –

11

檢查,如果你是一個返回@ResponseBody或@ResponseStatus

我也有類似的問題。我的控制器看起來就像是:

@RequestMapping(value="/user", method = RequestMethod.POST) 
public String updateUser(@RequestBody User user){ 
    return userService.updateUser(user).getId(); 
} 

當用POST請求,我總是有以下錯誤調用:

HTTP狀態405 - 請求方法「POST」不支持

過了一會我發現這個方法實際上是被調用的,但是因爲沒有@ResponseBody而沒有@ResponseStatus,所以Spring MVC引發了這個錯誤。

爲了解決這個問題簡直就是一個@ResponseBody

@RequestMapping(value="/user", method = RequestMethod.POST) 
public @ResponseBody String updateUser(@RequestBody User user){ 
    return userService.updateUser(user).getId(); 
} 

或@ResponseStatus添加到您的方法。

@RequestMapping(value="/user", method = RequestMethod.POST) 
@ResponseStatus(value=HttpStatus.OK) 
public String updateUser(@RequestBody User user){ 
    return userService.updateUser(user).getId(); 
} 
14

我不確定這是否有幫助,但我有同樣的問題。

您正在使用帶CSRF保護的springSecurityFilterChain。這意味着當您通過POST請求發送表單時,您必須發送令牌。嘗試下一個輸入添加到您的形式:

<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> 
+3

我不知道,謝謝。爲我工作 – mgokgoz

+0

如何溫和地禁用此過濾器鏈?因爲有很多方法需要改變,所以我希望它能夠先工作,然後逐步升級 –

+0

@YuJiaao - 您可以在Spring Security參考的第18.4.2章中找到它https://docs.spring.io/spring-security/site /docs/current/reference/html/csrf.html#csrf-configure –

相關問題