Enhance Groovy template support in CLI
to allow user to specify the TemplateEngine Fixes gh-908
This commit is contained in:
parent
7e9a4035db
commit
334c8142c4
|
@ -8,6 +8,7 @@ class Example implements CommandLineRunner {
|
||||||
@Autowired
|
@Autowired
|
||||||
private MyService myService
|
private MyService myService
|
||||||
|
|
||||||
|
@Override
|
||||||
void run(String... args) {
|
void run(String... args) {
|
||||||
print template("test.txt", ["message":myService.sayWorld()])
|
print template("test.txt", ["message":myService.sayWorld()])
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,7 +51,7 @@ public class SpringMvcCompilerAutoConfiguration extends CompilerAutoConfiguratio
|
||||||
"org.springframework.web.servlet.config.annotation",
|
"org.springframework.web.servlet.config.annotation",
|
||||||
"org.springframework.web.servlet",
|
"org.springframework.web.servlet",
|
||||||
"org.springframework.web.servlet.handler", "org.springframework.http",
|
"org.springframework.web.servlet.handler", "org.springframework.http",
|
||||||
"org.springframework.ui");
|
"org.springframework.ui", "groovy.text");
|
||||||
imports.addStaticImport(GroovyTemplate.class.getName(), "template");
|
imports.addStaticImport(GroovyTemplate.class.getName(), "template");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,11 +16,14 @@
|
||||||
|
|
||||||
package org.springframework.boot.groovy;
|
package org.springframework.boot.groovy;
|
||||||
|
|
||||||
|
import groovy.lang.Writable;
|
||||||
import groovy.text.GStringTemplateEngine;
|
import groovy.text.GStringTemplateEngine;
|
||||||
import groovy.text.Template;
|
import groovy.text.Template;
|
||||||
|
import groovy.text.TemplateEngine;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.StringWriter;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -41,7 +44,15 @@ public abstract class GroovyTemplate {
|
||||||
|
|
||||||
public static String template(String name, Map<String, ?> model) throws IOException,
|
public static String template(String name, Map<String, ?> model) throws IOException,
|
||||||
CompilationFailedException, ClassNotFoundException {
|
CompilationFailedException, ClassNotFoundException {
|
||||||
return getTemplate(name).make(model).toString();
|
return template(new GStringTemplateEngine(), name, model);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String template(TemplateEngine engine, String name, Map<String, ?> model)
|
||||||
|
throws IOException, CompilationFailedException, ClassNotFoundException {
|
||||||
|
Writable writable = getTemplate(name).make(model);
|
||||||
|
StringWriter result = new StringWriter();
|
||||||
|
writable.writeTo(result);
|
||||||
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Template getTemplate(String name) throws CompilationFailedException,
|
private static Template getTemplate(String name) throws CompilationFailedException,
|
||||||
|
|
Loading…
Reference in New Issue