Being a conservative JSF developer, I’ve been resorting to Ajax4JSF (A4J) for my ajaxified applications. All I need to do is just jack-in A4J into my application and extend from my existing JSF tags. So easy, I don’t need to know the underlying implementation of Ajax. But I’ve been thinking, what if one day, I have to develop or maintain an application without JSF? A4J will not be there anymore to help me out. So, I decided to look for a new utility which could facilitates in making an Ajax call. There are myriads of Ajax helper libraries, but one that does caught my attention is DWR. Current stable version is 2.0 but version 3.0 is under heavy development. So, we could expect the latter version anytime soon.
Why I like it so much? - because it makes Ajax implementation simple for Java developer. It actually generates the Javascript objects for client side, which kind of mimicking the Java objects on the server side.
Here is a simple example of DWR. Below is my Java object, which is a normal POJO with a method getting a User object.

Next, I just need to declare my POJO in dwr.xml file.

The convert tag, is where I declare my User bean class, so DWR will able to marshall and unmarshall the bean between client and server side. The create tag, is where I declare my POJO, which will be mapped to the generated DWR’s Javascript object.
Now server side configurations are out of my way. Lets go to the client side. In my page, I need to includes below scripts.
First script tag, imports the common DWR engine script which is a mandatory script to be imported. Second script tag, imports DWR utility script. Utility is optional but it provides lots of handy functions. Third script tag, is the generated script which contains the Javascript object that maps to my UserService POJO.
Now, I can just call the method from Javascript like below and let DWR do all the dirty work.

From the first function, you can see that I’m using the generated UserService object to make the Ajax call via getUser method. The way we call the method from a Javascript is a bit different compared to calling it from a Java class. Although, my method only accepts 1 argument in Java, but here, I have to put a callback function as the 2nd parameter which acts as the handler for the return object. DWR will handles all the heavy lifting on marshalling the request to the server and unmarshalling the response back to the Javascript object. The good thing is, not only you could pass and receive all the primitive and wrapper classes, but DWR also works on your POJO. As in this example, I’m expecting a User POJO as the return object. You don’t need to work on any XML or JSON format in marshalling and unmarshalling your data.
Although, comparing to A4J, there are more work to do here, especially on the client side, but it does give you more flexibility and control over your Ajax call. More control means you are able to fine tune your Ajax calls for performance optimization. Plus, DWR requires no specific framework. Hence, learning DWR, is beneficial because you could implement it regardless of your application architecture. It is also possible to patch DWR into your existing applications. DWR definitely will be in my list for my future projects.
source: dhydrated.wordpress
Related Stuff
-
MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.
-
Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone
-
TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support
-
ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.
-
Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.
-
Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software
- PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.
Be the first ... |Add your comment.
Your Comment ...
Name (required)
Email (required, hidden)
Website
