[英]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.