2016-10-20 5 views
0

以下の例外を取得するには、誰もがMongoDBのチュートリアルに示すように、その実際.ftl、ここで私が与えているhtmlファイル をもこの のためのソリューションを持っていると.ftlファイルを作成することができませんイムんFreeMarkerのエラーローディングテンプレート

package com.mongodb; 

    import freemarker.template.Configuration; 
    import freemarker.template.Template; 

    import java.io.StringWriter; 
    import java.util.HashMap; 
    import java.util.Map; 

    /** 
    * Created by tadoori on 10/20/2016. 
    */ 
    public class HelloworldFreemarkerStyle { 

     public static void main(String[] args) { 
      Configuration configuration = new Configuration(); 
      configuration.setClassForTemplateLoading(HelloworldFreemarkerStyle.class, "/"); 

      try { 

       Template helloTemplate = configuration.getTemplate("hello.html"); 
       StringWriter writer = new StringWriter(); 
       Map<String, Object> helloMap = new HashMap<String, Object>(); 
       helloMap.put("name", "Freemark"); 

       helloTemplate.process(helloMap, writer); 

       System.out.println(writer); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

これは、javaファイル私はIdea2016 whith作成した私のテストプロジェクトでこのコードを試してみました

java.io.FileNotFoundException: resources does not exist. 
    at freemarker.cache.FileTemplateLoader$1.run(FileTemplateLoader.java:125) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at freemarker.cache.FileTemplateLoader.<init>(FileTemplateLoader.java:122) 
    at freemarker.cache.FileTemplateLoader.<init>(FileTemplateLoader.java:108) 
    at com.mongodb.HelloworldFreemarkerStyle.main(HelloworldFreemarkerStyle.java:23) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 
+0

絶対パスをテンプレートまたはデバッグに渡して、どのパスが呼び出されているかを調べてみてください –

+0

絶対パス「C:\ Users \ tadoori \ M101J \ src \ resources \ hello.html」を渡した後、今度はjava.io.FilenotFoudExceptionと表示されます:リソースが存在しません。 – user6238728

+0

configuration.setClassForTemplateLoading(HelloworldFreemarkerStyle.class、 "C:\\ Users \\ tadoori \\ M101J \\ src \\ resources \\ hello.html"); – user6238728

答えて

0

(FreeMarkerのをv.2.3.23)とワークperfectyを実行するには以下の通りです。

import freemarker.template.Configuration; 
import freemarker.template.Template; 

import java.io.StringWriter; 
import java.util.HashMap; 
import java.util.Map; 

public class Main { 

    public static void main(String[] args) { 
     System.out.println("Hello World!"); 
     Configuration configuration = new Configuration(); 

     try { 

      Template helloTemplate = configuration.getTemplate("hello.html"); 
      StringWriter writer = new StringWriter(); 
      Map<String, Object> helloMap = new HashMap<String, Object>(); 
      helloMap.put("name", "Freemark"); 

      helloTemplate.process(helloMap, writer); 

      System.out.println(writer); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

ファイルhello.htmlはプロジェクトディレクトリのルートにあります。

+0

こんにちは、おかげで、応答は、実際には、Windows 7で私はWindows 10で試したことがあり、それは働いた – user6238728