2012-06-21 31 views
16

Im nhận được lỗi này: HTTP Status 405 - Request method 'POST' not supportedHTTP Status 405 - Yêu cầu phương pháp 'POST' không được hỗ trợ (Spring MVC)

Những gì tôi đang cố gắng làm là tạo ra một hình thức với một hộp thả xuống mà có được dân cư dựa trên khác giá trị được chọn trong một hộp thả xuống khác. Ví dụ: khi tôi chọn tên trong hộp customerName, chức năng onChange trong trang .jsp sẽ được chạy và trang được gửi sau đó được tải lại với các giá trị tương ứng trong hộp customerCountry.

tuy nhiên tôi nhận được lỗi trạng thái HTTP 405 này. Tôi đã tìm kiếm trên Internet một giải pháp nhưng không thể tìm thấy bất cứ thứ gì đã giúp. Dưới đây là những phần liên quan của mã của tôi:

phần của trang 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> 

một phần của điều khiển:

@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"; 
     } 

    } 

Tại sao tôi nhận được lỗi này? Bất kì sự trợ giúp nào đều được đánh giá cao! Cảm ơn

EDIT: Đã thay đổi hasId thành hasCustomerName. Tôi vẫn nhận được lỗi HTTP Status 405 - Request method 'POST' not supported.

EDIT2: nhận xét ra các dòng trong setFalse chức năng đã gây ra lỗi

// D

+1

Tại sao bỏ phiếu xuống? Vui lòng cung cấp cho tôi giải thích để tôi có thể đăng câu hỏi hay hơn trong tương lai. Cảm ơn – dlinx90

+0

cho những người gặp sự cố này và truy cập trang này, cũng kiểm tra xem hành động được xác định có đạt đến điểm cuối hiện tại hay không, đó là vấn đề của tôi :) –

Trả lời

3

tôi thấy vấn đề đã gây ra lỗi HTTP.

Trong chức năng setFalse() được kích hoạt bằng nút Lưu, mã của tôi đang cố gửi biểu mẫu chứa nút.

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

khi tôi loại bỏ các document.submitForm.submit(); nó hoạt động:

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

@Roger Lindsjö Cảm ơn bạn cho việc tìm kiếm lỗi của tôi, nơi tôi đã không đi qua trên các thông số đúng!

3

Vấn đề là bộ điều khiển của bạn mong đợi một tham số hasId = false hoặc hasId = đúng, nhưng bạn không vượt qua điều đó. Trường ẩn của bạn có id là hasId nhưng được chuyển thành hasCustomerName, vì vậy không có đối sánh bản đồ.

Hoặc thay đổi đường dẫn của trường ẩn để hasId hoặc tham số bản đồ để mong đợi hasCustomerName = true hoặc hasCustomerName = false.

+0

Tôi thấy tôi hơi mệt khi tôi viết bài này. Có mắt quan sát! Tôi vẫn nhận được chính xác lỗi tương tự mặc dù. Bất kỳ ý tưởng gì nó có thể được? – dlinx90

+1

Làm cách nào để thông báo lỗi gây hiểu nhầm? Nó dẫn chúng ta nghĩ rằng đó là một vấn đề động từ HTTP khi thực tế nó là một tham số. – Stephane

2

Bạn có thể cần phải thay đổi dòng

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

để

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

Không, có hai phương pháp, một cho mỗi RequestMethod. –

11

Kiểm tra nếu bạn đang trả lại một @ResponseBody hoặc một @ResponseStatus

Tôi đã có một vấn đề tương tự. Bộ điều khiển của tôi trông như thế:

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

Khi gọi với một yêu cầu POST tôi luôn có những lỗi sau đây:

HTTP Status 405 - Yêu cầu phương pháp 'POST' không được hỗ trợ

Sau một thời gian Tôi đã phát hiện ra rằng phương thức này thực sự được gọi, nhưng vì không có @ResponseBody và không có @ResponseStatus Spring MVC làm tăng lỗi.

Để sửa lỗi này chỉ cần thêm một @ResponseBody

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

hoặc một @ResponseStatus phương pháp của bạn.

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

Tôi không chắc liệu điều này có giúp ích hay không nhưng tôi cũng gặp phải vấn đề tương tự.

Bạn đang sử dụng springSecurityFilterChain với tính năng bảo vệ CSRF. Điều đó có nghĩa là bạn phải gửi mã thông báo khi gửi biểu mẫu qua yêu cầu POST. Hãy thử thêm đầu vào tiếp theo vào biểu mẫu của bạn:

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

Tôi không biết điều đó, cảm ơn. Làm việc cho tôi – mgokgoz

+0

Làm thế nào để vô hiệu hóa chuỗi bộ lọc này một cách nhiệt tình? bởi vì có rất nhiều phương pháp để thay đổi, tôi muốn nó đầu tiên hoạt động, sau đó nâng cấp dần dần –

+0

@YuJiaao - bạn có thể tìm thấy nó trong tài liệu tham khảo Spring Security, chương 18.4.2 https://docs.spring.io/spring-security/site /docs/current/reference/html/csrf.html#csrf-configure –

Các vấn đề liên quan