简体   繁体   中英

Request method 'POST' not supported- Spring:Bind

I have two Models- Customer and Address I passed both of the objects to the 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-

<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

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

Are you using spring security? Check the csrf setting of spring security.

By default, csrf defense is enabled to enable spring security. At this point, the solution is the same.

  1. You can send csrf token value in form.
<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
@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();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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