[1]必要なjarファイルの取得
Velocityをサーバサイドで利用するには、Velocityエンジン本体のほかにサブプロジェクトVelocity Toolsが必要です。
次のURLより、それぞれ配布バイナリvelocity-x.x.zip、velocity-tools-x.x.zipを取得します。

http://jakarta.apache.org/site/downloads/downloads_velocity.cgi

次に、ダウンロードした配布バイナリを解凍します。解凍されたフォルダから、セットアップに必要なjarファイルを以下の表の通り、Webアプリケーションルート配下の「WEB-INF\lib」フォルダにコピーします。

解凍フォルダファイル名
velocity-x.x velocity-x.x.jar
build\lib\commons-collections.jar
velocity-tools-x.x lib\velocity-tools-x.x.jar
lib\commons-beanutils-x.x.x.jar
lib\commons-digester-x.x.x.jar
lib\commons-lang-x.x.x.jar
lib\commons-logging-x.x.x.jar
▲セットアップに必要なjarファイル

[2]web.xmlの設定
以下のリストのようにWebアプリケーション配備記述子web.xmlを設定します。
まず、VelocityViewServletサーブレットを配備します。
次に、それをVelocityテンプレートの拡張子「.vm」に関連付けます。「.vm」というのは、Velocity Macroの略です。

[web.xml](抜粋)
  <web-app>
    ...中略...
    <!-- Velocityのサーブレットを配備する -->
    <servlet>
      <servlet-name>velocity</servlet-name>
      <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
      <init-param>
        <param-name>org.apache.velocity.toolbox</param-name>
        <param-value>/WEB-INF/velocity-toolbox.xml</param-value>
      </init-param>
      <init-param>
        <param-name>org.apache.velocity.properties</param-name>
        <param-value>/WEB-INF/velocity.properties</param-value>
      </init-param>
      <load-on-startup>10</load-on-startup>
    </servlet>
    ...中略...
    <!-- *.vmファイルをVelocityに関連付ける -->
    <servlet-mapping>
      <servlet-name>velocity</servlet-name>
      <url-pattern>*.vm</url-pattern>
    </servlet-mapping>
    ...中略...
  </web-app>
        

[3]velocity.propertiesの設定
velocity.propertiesは、Velocityエンジンのさまざまなパラメータを設定するプロパティファイルです。以下のリスト[velocity.properties]の内容のものを作成し、Webアプリケーションルート配下の「WEB-INF」フォルダに置いてください。
veloicity.propertiesで設定可能なプロパティのうち、よく使われるものは、以下の表[velocity.propertiesで設定可能な主なプロパティ]の通りです。

[velocity.properties]
  input.encoding = Windows-31J
  output.encoding = Windows-31J
  directive.foreach.counter.name = velocityCount
  directive.foreach.counter.initial.value = 1
  webapp.resource.loader.cache = false
      

プロパティ説明/th>
input.encoding Velocityテンプレートの文字コード
output.encoding Velocityが出力するHTMLの文字コード
directive.foreach.counter.name VTLの#foreachディレクティブ内で、現在のループ回数を参照するための変数名
directive.foreach.counter.initial.value ループ回数の初期値
webapp.resource.loader.cache レンダリングしたテンプレートをキャッシュするかどうか
runtime.log.logsystem.class Velocityが利用するログシステム。Velocityのログを無効にしたいときは、「org.apache.velocity.runtime.log.NullLogSystem」を指定する velocimacro.library マクロライブラリファイルの場所
▲velocity.propertiesで設定可能な主なプロパティ

[3]velocity-toolbox.xmlの設定
velocity-toolbox.xml(※)は、Velocityの拡張機能であるツールボックスの設定ファイルです。
以下のリスト[velocity-toolbox.xml]のファイルを用意し、Webアプリケーションルート配下の「WEB-INF」フォルダに置いてください。
以上でセットアップは完了です。

※Velocity本家サイトのユーザガイドでは、単にtoolbox.xmlと呼ばれています。しかし、これだとVelocityの設定ファイルであることが分かりにくく、アプリケーション保守の際に不要な混乱を招くことになります。そのため、ここではvelocity-toolbox.xmlという名前で紹介することにします。

[velocity-toolbox.xml]
  <?xml version="1.0" encoding="UTF-8" ?>
  <toolbox>
    <tool>
      <key>date</key>
      <scope>application</scope>
      <class>org.apache.velocity.tools.generic.DateTool</class>
    </tool>
    <tool>
      <key>esc</key>
      <scope>application</scope>
      <class>org.apache.velocity.tools.generic.EscapeTool</class>
    </tool>
    <tool>
      <key>number</key>
      <scope>application</scope>
      <class>org.apache.velocity.tools.generic.NumberTool</class>
    </tool>
    <tool>
      <key>params</key>
      <class>org.apache.velocity.tools.view.tools.ParameterParser</class>
    </tool>
    <tool>
      <key>link</key>
      <class>org.apache.velocity.tools.view.tools.LinkTool</class>
    </tool>
  </toolbox>
        
(文責:佐藤匡剛@WINGS)