Wednesday, October 19, 2005

On Demand AJAX and Templates

On most of my recent Java projects, I have been consistent in using
DWR, which is a great framework that is easy to use and quick to get
started with. On my recent trials, I had to prepare some of our
portlets to work on a Pocket PC browser (Internet Explorer).
Considering that our portlets depend on AJAX, I knew I was in for a treat.

I tried to use DWR but, it was throwing some object errors. Given I
was on a very, very tight schedule, I couldn't try and debug the
situation at that time. I reverted to rolling my own XHR solution which
turned out to be rather flexible.



I decided that I didn't want to deal with marshalling, passing, and
parsing XML, that is why I wanted to use DWR in the first place,
passing and parsing plain text would be just as bad. So what are my
options left. I decided to pass a JavaScript object, built by a servlet
on the back end, back to the client. The client simply passes the
responseText to eval() and "Wala" the data is now a convenient Javascript bean
that contains the needed data.



To get around the ugly part, creating the JavaScript object on the
back end, I decided to create templates of my JavaScript objects using
Velocity. This was great!



- Receive request



- Do some business logic



- Initialize Velocity and fill the context



- merge the template and pass it back in the response



Very clean and easy to do. I am going to look into replacing my XHR
js code and replace it with a dojo install that only has the IO
functionality in it. I have been playing with it and it rocks. I like
that you can choose to eval the return.



This can also be accomplished in other languages as well. I started doing the same thing with PHP, Dojo, and Smarty templates!

1 Comments:

At 12:49 PM, Anonymous Anonymous said...

I am interested in how you did this can you provide a sample source code. thx.

 

Post a Comment

<< Home