简体   繁体   中英

How to submit form without reloading the page using Spring Boot Async?

I can do it with Ajax when submitting the form and passing it through JQuery. I wonder if there's any alternative using Spring Boot @Async?

Here's my code so far, and as you can see the Post Request still returns a new ModelAndView page.

@GetMapping("/register")
public @Valid ModelAndView forPageScreen(@ModelAttribute("user") User user)
{   
    ModelAndView users = new ModelAndView("Practice");
    return users;
}


@Async
@Transactional
@PostMapping("/register")
public @Valid CompletableFuture<ModelAndView> addUser(@Valid @ModelAttribute("user") User user)
{
    ModelAndView mav = new ModelAndView("Practice");

    try {
        Thread.sleep(5000);
        userRep.save(user);

    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return CompletableFuture.completedFuture(mav);
}

@Async is for server side Async, not for the AJAX. That's whole different use-case

I can do it with Ajax when submitting the form and passing it through JQuery

That's the way to go. Async should be handled at the client side (JQuery in this case). As far as the server is concerned, there is nothing Async about that httpRequest (In this scenario)

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