请求方法“post”不受支持-spring:bind

fjaof16o  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(399)

我有两个模型-customer和address,我将这两个对象都传递给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;
    }

提交表单后,我想将这些对象存储到我的db-myjsp页面中-

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

单击create按钮后,我想调用一个post方法-

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/项目/新客户

t98cgbkg

t98cgbkg1#

你用的是spring安全吗?检查Spring安全装置的csrf设置。
默认情况下,csrf防御被启用以启用Spring Security 。在这一点上,解决方案是相同的。
您可以发送形式的csrf令牌值。

<input type = "hidden" name = "$ {_ csrf.parameterName}" value = "$ {_ csrf.token}"/>

如果要忽略csrf,可以将继承WebSecurityConfigureAdapter类的类设置为忽略某些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();
        }
    }
}

相关问题