Leandro Paiva
Leandro Paiva

Reputation: 23

Ajax send response to the view during method execution

Using Spring MVC I would like to update the view in an AJAX call to a long running method. Sorry if question may seem stupid but I do not know how to find an answer, if someone points me a tutorial how to do it would be extremely grateful.

Current running process:

  1. JSP calls a controller via AJAX
  2. Controller annotated with @ResponseBody executes the method and AT THE END OF THE EXECUTION OF THE METHOD returns an Object (in this case a Map with success message and number of recorded records) for the calling JQUERY function that exhibits success or error.

What I want to try to do:

  1. JSP calls a controller via AJAX
  2. Controller annotated with @ResponseBody executes the method and AS I AM PERFORMING THE METHOD (actually here I have a WHILE that writes a list of objects in the database, so this while can be time consuming or not depends on the number of objects to While I'm running this while I would like to send a response (but not in FINAL) to the view, I wanted to send a partial response for example: for each iteration in WHILE I want to send for example to the view the information: " Object.id ".

I want that in each iteration of the loop I send an information to the VIEW and that the jquery function with the calling request is able to render that information in a DIV tag.

If anyone can tell me some tutorial about it would be grateful.

Upvotes: 1

Views: 66

Answers (1)

Daniel Piskorz
Daniel Piskorz

Reputation: 440

HTTP is limited to one response per one request.

You can break the controller method, and adapt it to multiple requests that are necessary, if you want to have multiple responses. You would have to create some kind of iterator to keep up with this.

On this point you have to consider if it's really worth it, and if it is, reorganize the jQuery code too.

Upvotes: 1

Related Questions