Included with every WebCT installation is a handy little library that gives PowerLinks developers some very nice capabilities. I'm talking about the Velocity library. Velocity is a Java-based template engine. It allows you to create HTML pages that include references to Java objects, similar to server side scripting languages like PHP and JSP. Using Velocity with Proxy Tools/Auth Modules, you can create some very sophisticated PowerLinks.
How It Works
In your Java application, you create a Velocity context. Then, you put in the objects that you want to refer to in your HTML.
Then, you create an HTML template that contains VTL (Velocity Template Language) code. The VTL syntax is very simple and might seem a bit limited at first, but it actually contains everything it needs to do the job.
Finally, you "merge" the context objects with the template and you get back an HTML page, with the VTL code replaced with HTML.
What It Looks Like
First, you need to import the following classes:
import java.io.StringWriter;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.Template;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.*;
Next, you need to initialize a VelocityEngine object. For an authentication module, you could do this in the commit() method, since this is where you'll be generating the HTML page and calling setResponseContent()
VelocityEngine ve = new VelocityEngine();
java.util.Properties props = new java.util.Properties();
props.setProperty("resource.loader", "file, jar");
props.setProperty("file.resource.loader.path",
"./deployablecomponents/" + jar_base);
props.setProperty("jar.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.JarResourceLoader");
props.setProperty("jar.resource.loader.path",
"jar:[file:./deployablecomponents/]" + jar_base + "/VelocityModule.jar");
ve.init(props);
The resource.loader property specifies where your templates can be loaded from. In this case, we want to be able to load templates from both within our jar file and from the file system.
The file.resource.loader.path property specifies the base directory for loading templates from the file system.
The jar.resource.loader.path property specifies the jar from which templates can be loaded.
Next, you create a Velocity context and add objects to it.VelocityContext context = new VelocityContext();
context.put("request", super.getRequest());
context.put("settings", super.getSettings());
context.put("authModName", super.getSettingsGroupName());
context.put("author", super.getAuthor());
In this example, I added the HttpServletRequest object and the proxy tool's settings map as well as the auth mod's author and the name of the auth mod's settings group. The VelocityContext's putmethod takes two parameters. The first is the name that will be used to refer to the object from within the HTML template. The second is the object you are adding.
Finally, you merge the context objects with a template and and call setResponseContent() to display the HTML page.
Template template = ve.getTemplate(htmlTemplate);
StringWriter html = new StringWriter();
template.merge(context, "test.vm");
this.setResponseContent(html.toString());
return true;
The Template's merge method merges the context objects with the template test.vm (which, in this example, is located in the deployablecomponents directory) and returns an HTML file.
The template might look like this:
<html>
<head><title>Hello, $authModName</title></head>
<body>
<h1>Hello, $authModName\!</h1>
<p>The $authModName authentication module was written by $author.</p>
<p>The request contains the following parameters:</p>
\#foreach ($paramName in $request.getParameterNames())
$paramName = $request.getParameter($paramName)<br/>
\#end
</body>
</html>
Notice that the VTL object references begin with the dollar sign ($). Also notice the foreach that iterates over the parameter names in the request object. The actual HTML output will look like this:
<html>
<head><title>Hello, /DeployableComponents/VelocityModule</title></head>
<body>
<h1>Hello, /DeployableComponents/VelocityModule\!</h1>
<p>The /DeployableComponents/VelocityModule
authentication module was written by Paul.</p>
<p>The request contains the following parameters:</p>
id = 402050001<br/>
</body>
</html>
This is really just the tip of the iceberg. If you have Vista 4.0.1/CE 6.0.1, which both support calling PowerLinks web services from within deployable components, you can call web services like CalendarSDK and FileManagerSDK from within templates! We'll take a look at that in the next article in this series 
To learn more about Velocity and VTL, see the Velocity web site
.
 | This article originally authored by APOPOV on the WebCT DevNet
|