blob: 0b66a1a83c5f0156ceff888b53fd25338f020e24 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
Copyright 1999-2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Content Stylesheet for Site -->
<!-- start the processing -->
<!-- ====================================================================== -->
<!-- GENERATED FILE, DO NOT EDIT, EDIT THE XML FILE IN xdocs INSTEAD! -->
<!-- Main Page Section -->
<!-- ====================================================================== -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<meta name="author" value="Velocity Documentation Team">
<meta name="email" value="Paw Dybdahl (pdy@csg.csc.dk)">
<title>Velocity - Using Velocity With WebLogic</title>
</head>
<body bgcolor="#ffffff" text="#000000" link="#525D76">
<table border="0" width="100%" cellspacing="0">
<!-- TOP IMAGE -->
<tr>
<td align="left">
<a href="http://jakarta.apache.org"><img src="http://jakarta.apache.org/images/jakarta-logo.gif" border="0"/></a>
</td>
<td align="right">
<a href="http://jakarta.apache.org/velocity/"><img src="./images/logo.gif" alt="Velocity" border="0"/></a>
</td>
</tr>
</table>
<table border="0" width="100%" cellspacing="4">
<tr><td colspan="2">
<hr noshade="" size="1"/>
</td></tr>
<tr>
<!-- LEFT SIDE NAVIGATION -->
<td width="20%" valign="top" nowrap="true">
<!-- ============================================================ -->
<p><strong>About</strong></p>
<ul>
<li> <a href="./index.html">Overview</a>
</li>
<li> <a href="./getting-started.html">Getting Started</a>
</li>
<li> <a href="http://jakarta.apache.org/builds/jakarta-velocity/">Download</a>
</li>
<li> <a href="./install.html">Install</a>
</li>
<li> <a href="./design.html">Design</a>
</li>
<li> <a href="./contributors.html">Contributors</a>
</li>
<li> <a href="./changes.html">ChangeLog</a>
</li>
<li> <a href="./code-standards.html">Coding Standards</a>
</li>
<li> <a href="./license.html">License</a>
</li>
<li> <a href="./todo.html">TODO</a>
</li>
<li> <a href="http://issues.apache.org/bugzilla/enter_bug.cgi?product=Velocity">Report Issues</a>
</li>
</ul>
<p><strong>Community</strong></p>
<ul>
<li> <a href="./powered.html">Powered By Velocity</a>
</li>
<li> <a href="http://jakarta.apache.org/site/getinvolved.html">Get Involved</a>
</li>
<li> <a href="http://jakarta.apache.org/site/mail.html">Mailing Lists</a>
</li>
<li> <a href="http://jakarta.apache.org/site/cvsindex.html">CVS Repositories</a>
</li>
</ul>
<p><strong>Docs</strong></p>
<ul>
<li> <a href="./user-guide.html">User's Guide (English)</a>
</li>
<li> <a href="./user-guide_fi.html">User's Guide (Finnish)</a>
</li>
<li> <a href="./user-guide_fr.html">User's Guide (French)</a>
</li>
<li> <a href="./user-guide_es.html">User's Guide (Spanish)</a>
</li>
<li> <a href="./developer-guide.html">Developer's Guide</a>
</li>
<li> <a href="./vtl-reference-guide.html">VTL Reference Guide</a>
</li>
<li> <a href="./specification.html">Specification</a>
</li>
<li> <a href="./api/index.html">Javadoc</a>
</li>
</ul>
<p><strong>Tools</strong></p>
<ul>
<li> <a href="./tools/index.html">Velocity Tools</a>
</li>
<li> <a href="./anakia.html">Anakia : XML->doc tool</a>
</li>
<li> <a href="./texen.html">Texen : text generation</a>
</li>
<li> <a href="./dvsl/index.html">DVSL : XML xformation</a>
</li>
<li> <a href="./veltag.html">Veltag : JSP taglib</a>
</li>
<li> <a href="./migration.html">Migration to Velocity</a>
</li>
<li> <a href="./devtools.html">Editors and IDEs</a>
</li>
</ul>
<p><strong>Comparisons</strong></p>
<ul>
<li> <a href="./ymtd/ymtd.html">YMTD</a>
</li>
<li> <a href="./differences.html">VM/WM Differences</a>
</li>
<li> <a href="./casestudy1.html">JSP vs. Velocity</a>
</li>
<li> <a href="./casestudy2.html">XMLC vs. Velocity</a>
</li>
</ul>
<p><strong>Site Translations</strong></p>
<ul>
<li> <a href="http://jakarta.apache.org/velocity/">English</a>
</li>
<li> <a href="http://www.ingrid.org/jajakarta/velocity/velocity-1.2-rc2/docs-ja/index.html">Japanese</a>
</li>
</ul>
</td>
<td width="80%" align="left" valign="top">
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#525D76">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="Contents"><strong>Contents</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<ol>
<li>
<a href="velocity_and_weblogic.html#Assumptions">Assumptions</a>
</li>
<li>
<a href="velocity_and_weblogic.html#The Scope Of This Guide">The Scope Of This Guide</a>
</li>
<li>
<a href="velocity_and_weblogic.html#Where to Put the Template Files">
Where To Put the Template Files</a>
</li>
<li>
<a href="velocity_and_weblogic.html#Setting the Configuration Properties">
Setting the Configuration Properties</a>
</li>
<li>
<a href="velocity_and_weblogic.html#What About Stylesheets?">
What about stylesheets?</a>
</li>
<li>
<a href="velocity_and_weblogic.html#Where to Put the Velocity Jar">
Where to put the Velocity Jar</a>
</li>
<li>
<a href="velocity_and_weblogic.html#Making Life Easier for the Developer">
Making Life Easier for the Developer</a>
</li>
<li>
<a href="velocity_and_weblogic.html#A Build Process">
A Build Process</a>
</li>
</ol>
</blockquote>
</p>
</td></tr>
<tr><td><br/></td></tr>
</table>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#525D76">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="Assumptions"><strong>Assumptions</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<p>
This short guide can only be keept short because it assumes you already have
a working knowledge af Velocity and how to use it in the J2EE environment,
for example with Apache-Tomcat.
</p>
</blockquote>
</p>
</td></tr>
<tr><td><br/></td></tr>
</table>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#525D76">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="The Scope Of This Guide"><strong>The Scope Of This Guide</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<p>
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 <code>getRealPath()</code> method in
<code>ServletContext</code> returns the real path to the given directory.
</p>
<p>
Deploying the same war-file in WebLogic will not have the same effect,
since WebLogic keeps its deployed war-files unexploded, and the
<code>getRealPath()</code> method in <code>ServletContext</code>
returns <code>null</code>. So how should you
organize your properties file, templates, stylesheets, servlets and build
process in this environment?
</p>
<p>
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.
</p>
<p>
While all the relevant information can be found in the JavaDoc documentation,
this quick guide will help you get started quickly.
</p>
</blockquote>
</p>
</td></tr>
<tr><td><br/></td></tr>
</table>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#525D76">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="Where to Put the Template Files"><strong>Where to Put the Template Files</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<p>
Since the <code>ClasspathResourceLoader</code> 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 (<code>template.jar</code>
for example) in <code>WEB-INF/lib</code> which will make it available as a
resource in the classpath.
</p>
</blockquote>
</p>
</td></tr>
<tr><td><br/></td></tr>
</table>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#525D76">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="Setting the Configuration Properties"><strong>Setting the Configuration Properties</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<p>
Although the properties file could be given any name, for this guide we
will use <code>velocity.properties</code> since
all documentation and examples on the Velocity site uses this filename.
</p>
<p>
This file should include the following two lines to configure the resource
loader:
</p>
<pre>
resource.loader = class
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</pre>
<p>
The file can contain other configurations as well.
</p>
<strong>Where to put <code>velocity.properties</code></strong>
<p>
Put the <code>velocity.properties</code> file directly in the <code>WEB-INF</code>
directory and include the following lines in your <code>web.xml</code>:
</p>
<pre>
&lt;servlet&gt;
&lt;servlet-name&gt;...&lt;/servlet-name&gt;
&lt;servlet-class&gt;...&lt;/servlet-class&gt;
&lt;init-param&gt;
&lt;param-name&gt;properties&lt;/param-name&gt;
&lt;param-value&gt;/WEB-INF/velocity.properties&lt;/param-value&gt;
&lt;/init-param&gt;
&lt;/servlet&gt;
</pre>
<p>
and include the following implementation in your servlet(s):
</p>
<pre>
protected Properties loadConfiguration (ServletConfig config) throws IOException, FileNotFoundException {
String propsFile = config.getInitParameter(INIT_PROPS_KEY);
Properties p = new Properties();
if ( propsFile != null ) {
InputStream iStream = getServletContext().getResourceAsStream( propsFile );
if ( iStream != null ) {
p.load( iStream );
}
}
return p;
}
</pre>
</blockquote>
</p>
</td></tr>
<tr><td><br/></td></tr>
</table>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#525D76">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="What About Stylesheets?"><strong>What About Stylesheets?</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<p>
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.
</p>
<p>
I prefer to have my stylesheets keept in a directory called <code>stylesheets</code>
located under the root-directory of the web application (i.e. at the same
level as the <code>WEB-INF</code> directory).
</p>
<p>
In your template files you can reference the stylesheets like this:
</p>
<pre>
&lt;link REL="stylesheet" TYPE="text/css" HREF="./stylesheets/style.css"&gt;
</pre>
</blockquote>
</p>
</td></tr>
<tr><td><br/></td></tr>
</table>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#525D76">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="Where to Put the Velocity Jar"><strong>Where to Put the Velocity Jar</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<p>
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.
</p>
<p>
Putting the velocity jar in <code>WEB-INF/lib</code> your web application will result
in it's classes being available in the classpath for that web application.
</p>
</blockquote>
</p>
</td></tr>
<tr><td><br/></td></tr>
</table>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#525D76">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="Making Life Easier for the Developer"><strong>Making Life Easier for the Developer</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<p>
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 <code>MainServlet</code>, which all
application specific servlets can subclass. The only method the subclasses must
override is the <code>getTemplateName()</code> method, which should return the name of
this servlets template file.
</p>
<p>If the subclasses needs to put data into context (which most servlets do)
they can implement the <code>loadData()</code> method.
</p>
<p>
That's it. The rest is done by <code>MainServlet</code> (and <code>VelocityServlet</code> of course).
</p>
<pre>
public abstract class MainServlet extends VelocityServlet {
public Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx) {
loadData( request, response, ctx );
return getMyTemplate();
}
protected Properties loadConfiguration (ServletConfig config) throws IOException, FileNotFoundException {
String propsFile = config.getInitParameter(INIT_PROPS_KEY);
Properties p = new Properties();
if ( propsFile != null ) {
InputStream iStream = getServletContext().getResourceAsStream( propsFile );
if ( iStream != null ) {
p.load( iStream );
}
}
return p;
}
protected Template getMyTemplate( ) {
Template template = null;
try {
template = getTemplate( getTemplateName() + ".vm" );
}
catch (ParseErrorException pee) {
mylog("Parse error for template " + pee);
}
catch (ResourceNotFoundException rnfe) {
mylog("Template not found " + rnfe);
}
catch (Exception e) {
mylog("Error " + e);
}
return template;
}
/**
* Gets the name of the template file to be processed from this servlet. Should be
* overridden by every subclass.
*/
abstract protected String getTemplateName();
/**
* Load data into context.
*/
protected void loadData(HttpServletRequest request, HttpServletResponse response, Context ctx ) {
ctx.put( "dummy", "dummy" );
}
}
</pre>
</blockquote>
</p>
</td></tr>
<tr><td><br/></td></tr>
</table>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#525D76">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="A Build Process"><strong>A Build Process</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<p>
A simple build process that supports the outlined directory contains the
follwing steps:
</p>
<ol>
<li>Define shortcuts for the directories used during the build process</li>
<li>Prepare the build by coping all necessary files to the staging (build)
directory</li>
<li>Compile all java files</li>
<li>Make a jar containing all templates</li>
<li>Make a war containing the web application (in the distribution directory)</li>
</ol>
<p>
A concrete example following the above scheme:
</p>
<pre>
&lt;project name="carImportAdmin" default="all" basedir="."&gt;
&lt;!-- Source directory for deployment descriptors, manifest, properties,... --&gt;
&lt;property name="dd" value="./dd"/&gt;
&lt;!-- Work directory used during build process --&gt;
&lt;property name="build" value="./staging"/&gt;
&lt;!-- Target directory for the final war-file --&gt;
&lt;property name="dist" value="../build-ear/modules"/&gt;
&lt;!-- Source directory for all java files --&gt;
&lt;property name="src" value="./src"/&gt;
&lt;!-- Source directory for template files --&gt;
&lt;property name="templates" value="./templates"/&gt;
&lt;!-- Source directory for stylesheets --&gt;
&lt;property name="stylesheets" value="./stylesheets"/&gt;
&lt;!-- Libraries used in compile --&gt;
&lt;property name="lib" value="${dist}/CarImport.jar;${dist}/FPGuiden.jar;${dist}/velocity-dep-1.2-rc3.jar"/&gt;
&lt;target name="all" depends="init, compile, jar, war"/&gt;
&lt;target name="init" depends="clean"&gt;
&lt;tstamp/&gt;
&lt;mkdir dir="${build}"/&gt;
&lt;mkdir dir="${build}/stylesheets"/&gt;
&lt;mkdir dir="${build}/WEB-INF"/&gt;
&lt;mkdir dir="${build}/WEB-INF/classes"/&gt;
&lt;mkdir dir="${build}/WEB-INF/lib"/&gt;
&lt;copy todir="${build}/WEB-INF"&gt;
&lt;fileset dir="${dd}"&gt;
&lt;include name="velocity.properties"/&gt;
&lt;/fileset&gt;
&lt;fileset dir="${dd}/WEB-INF"&gt;
&lt;include name="web.xml"/&gt;
&lt;/fileset&gt;
&lt;/copy&gt;
&lt;copy todir="${build}/WEB-INF/lib"&gt;
&lt;fileset dir="${dist}"&gt;
&lt;include name="velocity-dep-1.2-rc3.jar"/&gt;
&lt;/fileset&gt;
&lt;/copy&gt;
&lt;copy todir="${build}/stylesheets"&gt;
&lt;fileset dir="${stylesheets}"&gt;
&lt;/fileset&gt;
&lt;/copy&gt;
&lt;/target&gt;
&lt;target name="compile"&gt;
&lt;javac srcdir="${src}" destdir="${build}/WEB-INF/classes" classpath="${CLASSPATH};${lib}"&gt;
&lt;/javac&gt;
&lt;/target&gt;
&lt;target name="jar"&gt;
&lt;jar jarfile="${build}/WEB-INF/lib/templates.jar"
basedir="${templates}"&gt;
&lt;/jar&gt;
&lt;/target&gt;
&lt;target name="war" depends="init"&gt;
&lt;jar jarfile="${dist}/carImportAdmin.war"
basedir="${build}"&gt;
&lt;/jar&gt;
&lt;/target&gt;
&lt;target name="clean"&gt;
&lt;delete dir="${build}"/&gt;
&lt;/target&gt;
&lt;/project&gt;
</pre>
</blockquote>
</p>
</td></tr>
<tr><td><br/></td></tr>
</table>
</td>
</tr>
<!-- FOOTER -->
<tr><td colspan="2">
<hr noshade="" size="1"/>
</td></tr>
<tr><td colspan="2">
<div align="center"><font color="#525D76" size="-1"><em>
Copyright &#169; 1999-2004, The Apache Software Foundation
</em></font></div>
</td></tr>
</table>
</body>
</html>
<!-- end the processing -->