2011-12-09 9 views
0

"クラスパス"リソースローダーに切り替えると、速度テンプレートにアクセスできないようです。私は/ WEB-INF/classes/templates/WEB-INF/templatesにtemplatesディレクトリを置いて、/ WEB-INF/libにtemplates.jarを作成しようとしました。それらのどれも働かない。何か案は?これらのファイルに対するアクセス許可はすべて正しいです。あなただけの私は過去にそれを行っているか、このクラスパスでベロシティテンプレートが見つかりません

答えて

1

IIRC、WEB-INF/clasesは、クラスパス3のルートです。それは働いていますが、それを働かせる最良の方法ではないかもしれません。

Properties prop = new Properties(); 
String absolutePath=new File(Thread.currentThread().getContextClassLoader().getResource("").getFile()).getParentFile().getParentFile().getPath();//this goes to webapps directory 
prop.put("file.resource.loader.path", absolutePath+"/WEB-INF/classes/templates"); 
Velocity.init(prop); 
Template t=Velocity.getTemplate("confirmation_html.vm"); 

p1ng、あなたが/webappsに/ WEB-INF/構造を持っていると仮定すると

+0

私は従わわかりません。ですから、私のテンプレートをWEB-INF/classes /に貼り付け、resourceLoaderPathを "templates /"に設定しますか?私はresourceLoaderPathが何もしていないことさえ確信しています。私は別の例でそれを見ただけです。 – Bradford

7

を「テンプレート/」または「/テンプレート」を試すことができるように

Properties p = new Properties(); 
p.setProperty("runtime.log.logsystem.class", "org.apache.velocity.tools.generic.log.CommonsLogLogSystem"); 

/* 
// Works fine: 
p.setProperty(RuntimeConstants.RESOURCE_LOADER, "file"); 
p.setProperty("file.resource.loader.path", "/path/to/templates"); 
*/ 

// Cannot find template with this: 
p.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); 
p.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName()); 
p.setProperty("resourceLoaderPath", "/WEB-INF/classes/templates"); 

org.apache.velocity.app.Velocity.init(p); 

template = org.apache.velocity.app.Velocity.getTemplate("confirmation_html.vm"); 
0

これは痛いです。クラスパスを置くと、速度テンプレートを変更するたびにサーブレットコンテナがwebappをリロードするため、開発が難しくなります。

org.apache.velocity.tools.view.WebappResourceLoaderを使用することをお勧めします。これは、ファイルをクラスパスに入れないようにすることで開発がはるかに簡単になり、相対インクルードも可能にします。

また、このことについての私の記事をチェックすることができます。Spring-mvc + Velocity + DCEVM

関連する問題