プロパティファイルはどんな名前でもよいのですが、Velocity
サイトの全てのドキュメントやサンプルで velocity.properties
という名前を使っているので、このガイドでもこのファイル名を使うことにします。
リソースローダを設定するため、このファイルには以下の2行が必要です。
resource.loader = class
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
このファイルには他の設定も含まれています。
velocity.properties はどこに置くか?
velocity.properties は WEB-INF
ディレクトリ直下に置き、web.xml に以下の数行を加えます。
<servlet>
<servlet-name>...</servlet-name>
<servlet-class>...</servlet-class>
<init-param>
<param-name>properties</param-name>
<param-value>/WEB-INF/velocity.properties</param-value>
</init-param>
</servlet>
そして、Servlet では以下のように実装します。
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;
}