Wednesday, October 10, 2007

Using Velocity Templates with AJAX


I am going to show a very simple example of how to use Ajax with your velocity templates .

I was using Turbine/Velocity framework for my project and was really wanted to use Ajax . So my first thought was why not create XML templates in Velocity . I found that its actually pretty easy to set that up .

First make sure that the framework treats the xml as a velocity template . For Turbine Velocity framework I changed the TR.props to the following

services.VelocityService.template.extension=vm,xml

Create the xml document that you can use in in AJAX .


<?xml version="1.0" encoding="UTF-8"?>
<devices>
#foreach($ditems in $devices)
<device id="$ditems.getDeviceid()" name="$!ditems.getName()">1</device>
#end
</devices>


then you can create a server side screen class for the xml .
make sure you setup the context type to xml

E,g from my Implementation

data.setContentType("text/xml");
context.put("devices",<Vector of devices>);

Compile and deploy it

Bingo ! You can test the xml from your browser by passing the necessary name/value pairs

1 comment:

Unknown said...

Thats quick. Keep up the turbine torch blaring..
cheers !