[英]Request method 'POST' not supported- Spring:Bind
我有兩個模型——客戶和地址我將兩個對象都傳遞給了 getMethod——
@RestController
public class NewCustomerController {
@Autowired
NewCustomerService newCustomerService;
@GetMapping(value = "newCustomer")
public ModelAndView newCustomer(){
ModelAndView modelAndView=new ModelAndView("views/customer/newCustomer");
modelAndView.addObject("customer",new Customer());
modelAndView.addObject("address",new Address());
return modelAndView;
}
提交表單后,我想將這些對象存儲到我的數據庫中-我的 JSP 頁-
<body class="container-fluid">
<jsp:include page="/navbar.jsp" />
<article>
<form action="newCustomer" method="post">
<spring:bind path="customer.customerCode">
<input type="text" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="customer.firstName">
<input type="text" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="customer.lastName">
<input type="text" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="customer.dateOfBirth">
<input type="date" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="customer.nationality">
<input type="text" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="customer.occupationType">
<input type="text" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="customer.totalWorkExperience">
<input type="text" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="customer.organizationName">
<input type="text" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<h2>ADDRESS</h2>
<spring:bind path="address.addressId">
<input type="text" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="address.houseNo">
<input type="text" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="address.city">
<input type="text" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="address.state">
<input type="text" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="address.country">
<input type="text" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<spring:bind path="address.pincode">
<input type="text" name="${status.expression}" value="${status.value}"><br />
</spring:bind>
<input type="submit" value="Create"/>
</form>
</article>
</body>
這是我想在單擊創建按鈕時調用的發布方法-
continue after get method...
@PostMapping(value = "newCustomer")
public ModelAndView addCustomer(@ModelAttribute("customer") Customer customer, BindingResult resultCustomer,@ModelAttribute("address") Address address,BindingResult resultAddress){
newCustomerService.createNewCustomer(customer);
newCustomerService.createNewAddress(address);
ModelAndView modelAndView=new ModelAndView("views/loanapplication/loanApplication");
return modelAndView;
}
}
單擊創建按鈕后-我收到此錯誤-不支持請求方法“POST”
作為參考,我在點擊創建按鈕之前顯示網址 - 點擊之前 - http://127.0.0.1:8080/Project/newCustomer點擊之后- http://127.0.0.1:8080/Project/newCustomer
您是否使用 spring 安全性? 檢查 spring 安全性的 csrf 設置。
默認情況下,啟用 csrf 防御以啟用 spring 安全性。 在這一點上,解決方案是相同的。
<input type = "hidden" name = "$ {_ csrf.parameterName}" value = "$ {_ csrf.token}"/>
@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {
@Value("${security.enable-csrf}")
private boolean csrfEnabled;
@Override
protected void configure(HttpSecurity http) throws Exception {
if (!csrfEnabled) {
http.csrf().disable();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.