2010-11-18 15 views

答えて

0

これは理想的ではないかもしれませんが、load the text of the zipped templateファイルの場合は、StringからFreeMarkerTemplateをインスタンス化できます。私はあなたにそれが下でどのように行われるかもしれないかの例を挙げましたが、私はthe freemarker documentationと読むことをお勧めします。私はzipファイルについては知らないが、あなたは「classForTemplateLoading」機能を使用してjarファイルからそれらをロードすることができ

Configuration cfg = new Configuration(); 
//configuring default free marker configuration 
cfg.setObjectWrapper(new DefaultObjectWrapper()); 

//construct template from string 
String templateText = "";//load template text from zip file 

Template template= new Template("sometemplate", new StringReader(templateText), cfg); 

//your data model 
Object root = new Object(); 

//process template 
StringWriter out = new StringWriter(); 
template.process(new Object(), out); 

String renderedText= out.toString(); 
0

すなわち

を(はじめに]タブを確認してください):

public class MyLoader 
{ 
    private static Configuration cfg = new Configuration(); 

    static 
    { 
     cfg.setClassForTemplateLoading(MyLoader.class, "/"); 
    } 

    public Template getTemplate(String path) throws Throwable 
    { 
     return cfg.getTemplate(path); 
    } 
} 

たとえば、テンプレート "MyTemplate.ftl"が 'com.mycode.templates'パッケージにある場合、パスは "/com/mycode/templates/MyTemplate.ftl"になります。

それで、あなたが 'ソース'ツリーをクラスであるかのように扱い、クラスパスにjarファイルを追加すれば、すべてうまくいくはずです。

+2

参照http://stackoverflow.com/questions/3019424/setting-freemarker-template-from-classpath –

関連する問題