Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ajax post,get, put, delete etc Vs html post and get only in spring pros and cons

I joined a project which uses spring framework and all the calls are made by ajax requests and the redirection after success is defined in the view itself and passed to the ajax JS function through a hidden input (so the return ModelAndView at the end of every function in the controller doesn't have any effect). I feel it messes up the code somehow Am I right? Still I think this was done because they wanted to get the benefits of having restful app with CRUD mapped to post,get,put,delete but eventually they lost the ability to redirect from the controller itself.

  • I want to know if there was other pattern to hold all that.
  • I also want to know the pros and cons of the previous way Vs using only GET and POST which easily allows redirection from the controller.
like image 287
Ismail Marmoush Avatar asked Oct 27 '12 15:10

Ismail Marmoush


2 Answers

Well the pattern which i generally use and recommend is the following:

  • User loads a page - Controller GET gets called and loads the view
  • On page load - AJAX script calls the POST of the controller to fetch the data from backend ( user sees a loader)
  • On success from POST request, the data is rendered.
  • On error returned - message is displayed to the user of any issues from the backend ( provides more control over redirection)

Advantages with this approach:

  1. Increased flexibility of error handling
  2. User doesn't have to wait for page to get loaded for data intensive pages
  3. Could be used as an hybrid approach where you can either use full web 2.0 feel or use a more traditional approach for certain operations.
like image 85
Liam Avatar answered Sep 17 '22 01:09

Liam


Ajax:

  • +no double posts on browser refresh
  • +client side execution
  • +less requests to the server
  • -additional security checks/configuration regarding XSS attacks

HTML:

  • +works in all browsers
  • +works when javascript is deactivated
  • -lack of usability in terms of speed

I just spend little time with Spring so I can not judge on everything. It may be that the development pattern itself of spring causes you to feel uncomfortable. In Java you are used to feel in OOP. The general concept of MVC gets mixed with html AJAX etc. Kept in mind that you have a server/client architecture and you would like to have all components distinct. Thats something that can be very well done with the Google Web Toolkit.

So what I read is that you make a browser refresh. Where is the benefit of AJAX if you make a refresh? Not knowing your application but knowing that some things can not be done so easy in Java (if you are adapting foreign code), you are doing right and should think over your program sequences.

The only diffrent way besides the AJAX HTML i can think of is a Socket connection that can be done with either an ActiveX Component, Flash or the html5 Websockets. But thats generally not what you use for simple forms.

BTW. the GET string is known to be maximum around 2000 chars but a bit faster in execution because you don't send headers like POST.

And in my oppinion: Regarding to performance, its better to have less requests and spit out more html first than try and force yourself to just make ajax everywhere. Since you lost your SEO advantage anyway.....

like image 43
Dr. Dama Avatar answered Sep 21 '22 01:09

Dr. Dama