2016-07-30 48 views
2

我想通過groovy.text.StreamingTemplateEngine(Groovy v2.4.7)處理一個非常簡單的模板。這裏是模板文件的全部內容:Groovy模板解析錯誤:未知類型:IMPORT

<% import org.yaml.snakeyaml.Yaml %> 

我知道這不會產生任何輸出。我只是想讓導入工作。

我得到這個錯誤:

Caught: groovy.text.TemplateParseException: Template parse error 'Unknown type: IMPORT at line: 1 column: 146. File: StreamingTemplateScript1.groovy ' at line 1, column 4 
--> 1: <% import org.yaml.snakeyaml.Yaml %> 

這裏是在做加工的Groovy代碼:

#!/usr/bin/env groovy 

def engine = new groovy.text.StreamingTemplateEngine() 
def tmplt 

if (args.length == 1) { 
    tmplt = engine.createTemplate(new File(args[0])).make() 
} 
else { 
    tmplt = engine.createTemplate(new BufferedReader(new InputStreamReader(System.in))).make() 
} 

println tmplt.toString() 

從我瞭解的Groovy模板,包括在常規腳本程序導入該模板將無法工作(也嘗試過)。我見過其他人(顯然)成功地做了我想要做的事情。

我在做什麼錯?

請注意,我在grails之外進行此操作。

回答

0

回答了我自己的問題。根據對StreamingTemplateEngine Groovy的文檔:

This engine has equivalent functionality to the SimpleTemplateEngine but creates the template using writable closures making it more scalable for large templates.

這顯然是不正確的,因爲它不明白import。要在Groovy模板中導入庫,您必須改用SimpleTemplateEngine。

我通過設置Apache Tomcat和Groovy Server Page(w/o grails)來了解GSP是否可以導入數據。他們能。 GSP使用https://github.com/groovy/groovy-core/blob/master/subprojects/groovy-servlet/src/main/java/groovy/servlet/TemplateServlet.java,它使用SimpleTemplateEngine。