7

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.
Ismail Marmoush
  • 11,912
  • 23
  • 73
  • 109

3 Answers3

6

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.
Abhi
  • 2,779
  • 20
  • 36
2

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.....

Dr. Dama
  • 171
  • 2
  • 5
0

Web has matured a lot over the years. The traditional HTTP Request Types like PUT,DELETE were not useful to majority of circumstances in web development and hence their use was minimal.

It is advisable to use only GET & PUT design patterns in your Web Application Development approach. Wherever you just need to send one or atmost two input parameters to get response you should use GET, whenever you need to send in more than 2 parameters for input to a HTTP Resource i.e. URI then you can use POST

Rahat Khanna
  • 4,330
  • 4
  • 18
  • 31