We pull data from server and for that If we are using Struts, then we can pull either by submitting a page which MVC Architecture or we cam make an AJAX call but conventions is of using forms and render response but we also face challenges to give rich user experience, so we compromise convention and start using excessive AJAX, so how we should make balance between two?


Solution 1:

I personally think that AJAX should be used for displays updates and form submissions should be done via a page reload. Reasoning?

When submitting forms, you are telling the application to do something. Users tend to want to feel that it was done. When a page doesn't reload, users are often left wondering "Did that work?". Then they have to check to make sure what they did was right.

On the other hand, when you are displaying a chart or something, and the user says to "display 2011 data....now 2012 data" for instance, they aren't "doing" something (creating new entities, sending emails, etc). So AJAX can provide a nice user interface in this case. Page reloads would be annoying here.

In conclusion, I think form submission should be done via page reloads (let the user see it working), whereas display updates should use AJAX (prevent annoying page reloads).

Of course, this is a preference thing. Some of my company's applications use AJAX all over. But those are the applications that are the most difficult to maintain and debug. ;)

Solution 2:

Regular old HTML form submission and fancy ajax forms are not mutually exclusive.

First, make the plain HTML form work correctly. Then, add javascript to hijack the form and send an ajax request.

The controller and model don't care if the user's browser supports (or has enabled) javascript. The rendered view is decided by whether the call was made with javascript or a simple form submission. This is one of the strengths of the MVC pattern, not a constraint.

Solution 3:

I think that the choice between the two is somewhat intrinsic:

  • a form submission is synchronous and it reloads the page.

  • an ajax call is asynchronous and it does not reload the page.

If a certain action will change a lot of UI elements or needs to poll a lot of data to be rendered, I would go with form submission. On the other hand, if a certain action is used for simple actions, like populating a select box or improving user experience, then I would go for an AJAX call.

There is nothing avoiding you to use as many ajax calls or form submissions as you need, so in the end is up to you.