具有AJAX集成的Spring

So I'm able to successfully integrate AJAX requests with Spring MVC. However, I have a problem- if I click the "submit" button of my form, my @Controller class detects the url and returns a ModelAndView. However, what I want is that there be an AJAX check first, and if the form submission is not successful (e.g., blank fields), return an AJAX response. Otherwise, proceed as per normal and display a ModelAndView. However, I have no clue how to integrate both at the same time.

Any ideas or tutorials are appreciated.

Thanks!

You have several choices:

  • submit the form to a specific, different URL, when using AJAX
  • add a specific parameter to the request when posting using AJAX, and use this specific parameter to check if the request is n AJAX request
  • test if the X-Requested-With request header is present and contains XMLHttpRequest

I would go the PJAX route or what's also known as HiJax.

Basically you return a subset of the page if it's an AJAX request using headers. Most people than just use conditions in their view/template to decide to include the full or chrome-less HTML.