Im uzyskiwanie ten błąd: HTTP Status 405 - Request method 'POST' not supported
HTTP 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
Dlaczego głosowanie w dół? Proszę podać wyjaśnienie, aby móc zamieszczać lepsze pytania w przyszłości. Dzięki – dlinx90
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 :) –