2016-10-20 76 views
0

,提示以下異常,沒有任何人有這個 也解決方案,如圖MongoDB的教程,它實際上.ftl,在這裏我給HTML文件 和IM無法創建.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文件

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

我試圖在我的測試項目驗證碼蒙山Idea2016創建(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位於項目目錄root。

+0

嗨,謝謝,事實上它的問題與Windows 7我猜,因爲我試過在Windows 10中,它的工作 – user6238728