![](/img/trans.png)
[英]HTTP Status 405 - Request method 'POST' not supported Spring MVC
[英]HTTP Status 405 - Request method 'POST' not supported (Spring MVC)
我收到此錯誤: HTTP Status 405 - Request method 'POST' not supported
我想要做的是制作一個帶有下拉框的表單,該下拉框根據在另一個下拉框中選擇的其他值進行填充。 例如,當我在customerName
框中選擇一個名稱時,.jsp 頁面中的onChange
函數應該運行,然后提交的頁面再次加載customerCountry
框中的相應值。
但是我收到了這個 HTTP Status 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";
}
}
我怎么會收到這個錯誤? 任何幫助將非常感激! 謝謝
編輯:將hasId
更改為hasCustomerName
。 我仍然收到HTTP Status 405 - Request method 'POST' not supported
錯誤。
EDIT2:注釋掉導致錯誤的setFalse
函數中的行
// D
我不確定這是否有幫助,但我遇到了同樣的問題。
您正在使用具有 CSRF 保護的 springSecurityFilterChain。 這意味着當您通過 POST 請求發送表單時,您必須發送一個令牌。 嘗試將下一個輸入添加到您的表單中:
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
檢查您返回的是 @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();
}
您可能需要更改行
@RequestMapping(value = "/add", method = RequestMethod.GET)
到
@RequestMapping(value = "/add", method = {RequestMethod.GET,RequestMethod.POST})
問題是您的控制器需要一個參數hasId=false或hasId=true ,但您沒有傳遞它。 您的隱藏字段具有 id hasId但作為hasCustomerName傳遞,因此沒有映射匹配。
將隱藏字段的路徑更改為hasId或將映射參數更改為期望hasCustomerName=true或hasCustomerName=false 。
我發現了導致 HTTP 錯誤的問題。
在由“保存”按鈕觸發的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()
@Roger Lindsjö 感謝您發現我沒有傳遞正確參數的錯誤!
由於其他原因,我遇到了類似的問題(未在 csrf 令牌中添加 url 模式test-response
)我通過在config/local.properties
的以下屬性中允許我的 URL 模式來解決它:
csrf.allowed.url.patterns = /[^/]+(/[^?])+(sop-response)$,/[^/]+(/[^?])+(merchant_callback)$,/[^/]+(/[^?])+(hop-response)$
修改為
csrf.allowed.url.patterns = /[^/]+(/[^?])+(sop-response)$,/[^/]+(/[^?])+(merchant_callback)$,/[^/]+(/[^?])+(hop-response)$,/[^/]+(/[^?])+(test-response)$
在我的例子中,url 以 / 結尾
paymentUrl(old)= /get-details/
我剛剛刪除了尾隨/
paymentUrl(new)= /get-詳細信息
它奏效了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.