In this article, Joel Confino explains how existing web pages can be augmented to make use of AJAX by using jQuery, a JavaScript library. He also shows how to retrieve data as XML instead of HTML by using Spring with XStream/Jettison.
Read: Your First Cup of Web 2.0 - A Quick Look at jQuery, Spring MVC, and XStream/Jettison
Many web pages use AJAX to connect to the server in order to bring new data to be displayed without refreshing the entire page. The constant page refresh is annoying for the user, so it is desirable to be avoided. But there are still many older sites/pages which still refresh the page when data is requested, or the user clicks a button. Updating all those pages to use AJAX can be a time consuming task. Using jQuery, the developer can transform existing pages to use AJAX with little coding.
In his article, Joel explains how to use jQuery on the client, and he also shows how to return data as XML instead of HTML by using Spring with XStream/Jettison, which can improve how portions of a page are refreshed, making the pages more responsive and avoiding the annoying page refresh.
Community comments
How does this compare to the Ajax support that is in Spring MVC?
by Jacek Furmankiewicz,
Re: How does this compare to the Ajax support that is in Spring MVC?
by Solomon Duskis,
Re: How does this compare to the Ajax support that is in Spring MVC?
by Joel Confino,
Authentication
by Gabriel Silva,
Re: Authentication
by Joel Confino,
Example Code Posted
by Joel Confino,
Comparison to other JSon/Data Server side technologies
by Solomon Duskis,
Re: Comparison to other JSon/Data Server side technologies
by Ayan Barua,
Questions
by Ilan M,
Download lInk
by Carlos Tevez,
How does this compare to the Ajax support that is in Spring MVC?
by Jacek Furmankiewicz,
Your message is awaiting moderation. Thank you for participating in the discussion.
I was just reading that Spring MVC 2.0 added a whole Javascript support module (based on Dojo).
How would you describe your jQuery option compared to that default functionality?
Re: How does this compare to the Ajax support that is in Spring MVC?
by Solomon Duskis,
Your message is awaiting moderation. Thank you for participating in the discussion.
The Spring MVC JS support that you're talking about is based off of Spring 2.5+. I built something similar to Joel's framework. I've also looked into the Spring-JS stuff that's in the Spring Web Flow package.
Basically, Spring-JS is an integration between Spring MVC and Tiles that only renders a partial HTML fragment rather than the whole page. The JS/Dojo portion calls the original controller and tells it which parts to render, and then how to place that HTML fragment back into the current page. Spring-JS is not in the Spring core package, and has to be downloaded as part of the Spring Web Flow package.
Joel's approach uses JSon rather than HTML fragments. It also uses JQuery's built in DOM manipulation. It's more network friendly, but you may end up with more cross-browser issues, since you're doing a lot of DOM manipulation. JQuery handles a lot of issues, but not everything.
JQuery seems to be a favorite of UI developers who are more comfortable in JS. Dojo seems to be a favorite of the Java/back-end guys.
Authentication
by Gabriel Silva,
Your message is awaiting moderation. Thank you for participating in the discussion.
Hello,
What if user looses his/her session and then tries to use the system. How would you handle such situation? Is there any built-in solution in JQuery to handle that?
Thank you for the article.
Re: How does this compare to the Ajax support that is in Spring MVC?
by Joel Confino,
Your message is awaiting moderation. Thank you for participating in the discussion.
Thanks Solomon for the good summary of Spring JS. I am new to Spring JS myself (2.0 was just released in April), but I agree with your assessment.
So how do jQuery and Spring JS compare? I think they overlap. jQuery is more flexible and general purpose, but Spring JS looks promising and provides some features that jQuery doesn't (more on that below).
Spring JS is geared towards the server returning HTML snippets. That architecture has advantages and disadvanges like any other. One really nice advantage is that Spring JS will degrade nicely if the browser doesn't support JavaScript. jQuery can also be used to return HTML snippets, but obviously can only function in browsers that run JavaScript. While Spring JS currently uses Dojo as the underlying JavaScript framework, the documentation states that jQuery *may* be supported in the future -- guess we'll see.
I personally like the server returning data instead of HTML, and to do that you'll need a framework like jQuery or Flex or GWT on the front end. A major advantage of that architecture is that the UI and server are completely decoupled. The XML/JSON document becomes the contract between them, and often the server implements a REST-based API. This allows them to be developed on different schedules independently. So UI folks can immediately develop the UI using a static XML file for data and don't have to wait for the server to be developed. Likewise the server can be developed and tested without needing the UI.
Re: Authentication
by Joel Confino,
Your message is awaiting moderation. Thank you for participating in the discussion.
Just to expand your question a little bit, I'll rephrase it as, "What happens when you make an AJAX call and you are not authenticated with the server?"
In my experience there is no built-in standard solution in jQuery to handle authentication. However the custom solution is not too difficult. After making an AJAX call, you check the server response in a callback function. If the response from the server is "Hey you're not authenticated" you show the user a login dialog or something, and send the username/password to the server in another AJAX call. It is actually quite common to need to do some custom stuff before and after AJAX calls (like checking the response for something after a call or checking a local cache before making a request), and jQuery is extensible and makes it easy to make your own custom functions and have them be available in the jQuery "$." namespace. So instead of calling jQuery's $.getJSON function you call your custom $.getJSONCustom function which does additional work and delegates to jQuery's $.getJSON for the actual AJAX call.
Example Code Posted
by Joel Confino,
Your message is awaiting moderation. Thank you for participating in the discussion.
I posted the full example code at: www.chariotsolutions.com/code_samples/car-demo-...
Comparison to other JSon/Data Server side technologies
by Solomon Duskis,
Your message is awaiting moderation. Thank you for participating in the discussion.
There was some discussion about Browser-side technologies (Dojo vs. JQuery). I'd also like to throw in some other technologies that can return server-side JSon/XML and integrate with Spring MVC:
1. The next version of Spring 3.0 MVC will have more support to do this, including more "RESTful URLs." The current model is limited to "/models.html?modelId=1010." Spring MVC 3.0 will allow you to map "/models/1010.json" to your controller. All of this is still in progresss, but it has to be out before Spring One - just take a look at all of the Web and REST sessions in the schedule
2. JAX-RS - It's a JEE standard API for "Web Services." It has a similar annotation-driven model. Jersey, the reference implementation of JAX-RS, integrates with Spring. It has the concepts of JSonView, XmlView, YourOwnView, already built in
3. DWR - Direct Web Remoting. It's a framework that's been around for quite a while. It has a server-side component and it's own JavaScript framework. It has some pretty advanced performance and security features
Re: Comparison to other JSon/Data Server side technologies
by Ayan Barua,
Your message is awaiting moderation. Thank you for participating in the discussion.
Might be bit of a stupid question here, but will this work with Hibernate 3+?
I am looking at a jquery/jsonview/spring/hibernate/mysql stack hence the question.
We tried to use JSONView from another project and apparently there were some conflicts.
What is your take on this architecture? I would much appreciate if you throw some light on this.
Thanks
Ayan
Questions
by Ilan M,
Your message is awaiting moderation. Thank you for participating in the discussion.
1. I understand that the demo carsearchresults.html is ajax with out JSON. Correct?
2. I tried to the same as the carsearchresults.html example in my code. The action took place, it returned to the url with carsearch.html page but showning only the context of carsearchresults.html. Do you have an idea what I have to do? The only different in my code is that I am using a formcontroller.
Download lInk
by Carlos Tevez,
Your message is awaiting moderation. Thank you for participating in the discussion.
hello, could you please update the download link?
it doesn't work anymore.
I'm really interested in the example code.
Thanks
reseba