Using Velocity With WebLogic
Paw Dybdahl (firstname.lastname@example.org)
- The Scope Of This Guide
- Where To Put the Template Files
- Setting the Configuration Properties
- What about stylesheets?
- Where to put the Velocity Jar
- Making Life Easier for the Developer
- A Build Process
This short guide can only be kept short because it assumes you already have a working knowledge of Velocity and how to use it in the J2EE environment, for example with Apache-Tomcat.
The Scope Of This Guide
Deploying web-applications to Tomcat can be accomplished by a war-file, but at startup Tomcat explodes the war-file so the directory structure are available for the Velocity engine at runtime and the
getRealPath() method in
ServletContext returns the real path to the given directory.
Deploying the same war-file in WebLogic will not have the same effect, since WebLogic keeps its deployed war-files unexploded, and the
getRealPath() method in
null. So how should you organize your properties file, templates, stylesheets, servlets and build process in this environment?
The scope of this little guide is to give you answers to these questions, so you can enjoy using Velocity even in the context of a commercial app server.
While all the relevant information can be found in the Javadoc documentation, this quick guide will help you get started quickly.
Where to Put the Template Files
ClasspathResourceLoader can load a template from a resource found in the servlet engines classpath, it seems like a very good idea to jar all the template-files together and place this jar (
for example) in
WEB-INF/lib which will make it available as a resource in the classpath.
Setting the Configuration Properties
Although the properties file could be given any name, for this guide we will use
velocity.properties since all documentation and examples on the Velocity site uses this filename.
This file should include the following two lines to configure the resource loader:
The file can contain other configurations as well.
Where to put velocity.properties?
velocity.properties file directly in the
directory and include the following lines in your
and include the following implementation in your servlet(s):
What About Stylesheets?
If you have any stylesheets you can place them anywhere you like, as long as you only make relative references to these files from your template files.
I prefer to have my stylesheets keept in a directory called
located under the root-directory of the web application (i.e. at the same level as the
In your template files you can reference the stylesheets like this:
Where to Put the Velocity Jar
First of all you have to decide whether you will use the dependency-free version of velocity.jar or the version including all dependend jars. If you are not worried about collisions with Avalon Logkit, Commons Collections or Jakarta Oro, using the jar containing all dependencies is very convenient.
Putting the velocity jar in
WEB-INF/lib your web application will result in it's classes being available in the classpath for that web application.
Making Life Easier for the Developer
Using Velocity in 'real life' you quickly recognize a lot of similarity between your servlets. The first abstraction that will make life a little easier for the developers is a servlet like my
MainServlet, which all application specific servlets can subclass. The only method the subclasses must override is the
getTemplateName() method, which should return the name of this servlets template file.
If the subclasses needs to put data into context (which most servlets do) they can implement the
That's it. The rest is done by
VelocityServlet of course).
A Build Process
A simple build process that supports the outlined directory contains the following steps:
- Define shortcuts for the directories used during the build process 2. Prepare the build by coping all necessary files to the staging (build) directory 3. Compile all java files 4. Make a jar containing all templates 5. Make a war containing the web application (in the distribution directory)
A concrete example following the above scheme: