简体   繁体   English

不支持请求方法“POST”- Spring:Bind

[英]Request method 'POST' not supported- Spring:Bind

I have two Models- Customer and Address I passed both of the objects to the getMethod-我有两个模型——客户和地址我将两个对象都传递给了 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;
    }

After submitting a form i want to store these objects into my db- My JSP Page-提交表单后,我想将这些对象存储到我的数据库中-我的 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>

Here is a post method i want to call on clicking the create button-这是我想在单击创建按钮时调用的发布方法-

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

After clicking on create button- I am getting this error- Request method 'POST' not supported单击创建按钮后-我收到此错误-不支持请求方法“POST”

For reference i am showing the urls after before clicking create button- before click- http://127.0.0.1:8080/Project/newCustomer After click- http://127.0.0.1:8080/Project/newCustomer作为参考,我在点击创建按钮之前显示网址 - 点击之前 - http://127.0.0.1:8080/Project/newCustomer点击之后- http://127.0.0.1:8080/Project/newCustomer

Are you using spring security?您是否使用 spring 安全性? Check the csrf setting of spring security.检查 spring 安全性的 csrf 设置。

By default, csrf defense is enabled to enable spring security.默认情况下,启用 csrf 防御以启用 spring 安全性。 At this point, the solution is the same.在这一点上,解决方案是相同的。

  1. You can send csrf token value in form.您可以在表单中发送 csrf 令牌值。
<input type = "hidden" name = "$ {_ csrf.parameterName}" value = "$ {_ csrf.token}"/>
  1. If you want to ignore csrf, you can set a class that inherits WebSecurityConfigurerAdapter class to ignore certain URLs如果要忽略 csrf,可以设置继承 WebSecurityConfigurerAdapter class 的 class 以忽略某些 URL
@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM