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.
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:
- Increased flexibility of error handling
- User doesn't have to wait for page to get loaded for data intensive pages
- 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.