2012-06-21 11 views
16

Im uzyskiwanie ten błąd: HTTP Status 405 - Request method 'POST' not supportedHTTP status 405 - metoda żądanie „POST” nie jest obsługiwana (wiosna MVC)

Co staram się zrobić to formularz z listy rozwijanej, które uzyskać zaludnionych w oparciu o drugą wartość wybrana w innym polu rozwijanym. Na przykład po wybraniu nazwy w polu customerName należy uruchomić funkcję onChange na stronie .jsp, a następnie przesłać stronę, a następnie ponownie załadować odpowiednie wartości w polu customerCountry.

Jednak otrzymuję błąd HTTP Status 405. Szukałem rozwiązania w Internecie, ale nie znalazłem niczego, co pomogło. Oto odpowiednie części mojego kodu:

część strony 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> 

część kontrolera:

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

    } 

Jak to się stało, że ten błąd? Każda pomoc będzie doceniona! Dzięki

EDYCJA: Zmieniono hasId na hasCustomerName. Nadal dostaję błąd HTTP Status 405 - Request method 'POST' not supported.

EDIT2: wykomentowane linii w funkcji setFalse który był przyczyną błędu

// D

+1

Dlaczego głosowanie w dół? Proszę podać wyjaśnienie, aby móc zamieszczać lepsze pytania w przyszłości. Dzięki – dlinx90

+0

osobom, które trafiły na ten problem i przychodzą na tę stronę, sprawdź także, czy zdefiniowana czynność trafiła w istniejący punkt końcowy, to był mój problem :) –

Odpowiedz

3

znalazłem problem, który powodował błąd HTTP.

W funkcji setFalse(), która jest wywoływana przez przycisk Zapisz, mój kod próbował przesłać formularz zawierający przycisk.

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

kiedy wyjąć document.submitForm.submit(); to działa:

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

@Roger Lindsjö Dziękuję za plamienia mój błąd, gdzie nie było przekazywanie właściwym parametrem!

3

Problem polega na tym, że kontroler oczekiwać parametrem chasyd = false lub chasyd = prawda, ale nie przekazujesz tego. Twoje ukryte pole ma identyfikator posiadający identyfikator, ale jest przekazywany jako hasCustomerName, więc nie pasuje do mapowania.

Albo zmienić ścieżkę pola ukryte chasyd lub parametru mapowania się spodziewać hasCustomerName = true lub hasCustomerName = false.

+0

Widzę, że byłem trochę zmęczony, kiedy to pisałem. Dobre oko! Nadal jednak otrzymuję dokładnie ten sam błąd. Jakieś pomysły, jakie to może być? – dlinx90

+1

Dlaczego komunikat o błędzie jest tak mylący? To prowadzi nas do myślenia, że ​​jest to problem z czasownikiem HTTP, gdy w rzeczywistości jest to parametr jeden. – Stephane

2

Być może trzeba zmienić linię

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

do

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

Nie, istnieją dwie metody, po jednej dla każdego RequestMethod. –

11

Sprawdź, czy jesteś przekazujących @ResponseBody lub @ResponseStatus

miałem podobny problem. Mój kontroler wyglądało tak:

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

Dzwoniąc z żądania POST zawsze uzyskałem następujący błąd:

HTTP status 405 - Zapytanie metoda 'POST' nie jest obsługiwana

Po pewnym czasie Stwierdziłem, że metoda została faktycznie wywołana, ale ponieważ nie ma @ResponseBody i nie @ResponseStatus Spring MVC podnosi błąd.

Aby rozwiązać ten problem wystarczy dodać @ResponseBody

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

lub @ResponseStatus do swojej metody.

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

Nie jestem pewien, czy to pomaga, ale miałem ten sam problem.

Używasz SpringSecurityFilterChain z zabezpieczeniem CSRF. Oznacza to, że musisz przesłać token, gdy wysyłasz formularz za pośrednictwem żądania POST. Spróbuj dodać kolejne dane wejściowe do formularza:

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

Nie wiedziałem tego, dzięki. Pracowałem dla mnie – mgokgoz

+0

Jak tymczasowo wyłączyć ten łańcuch filtrów? ponieważ mam tyle metod do zmiany, chcę, żeby to zadziałało, a następnie stopniowo uaktualniam –

+0

@YuJiaao - można je znaleźć w źródle Spring Security, rozdział 18.4.2 https://docs.spring.io/spring-security/site /docs/current/reference/html/csrf.html#csrf-configure –

Powiązane problemy