mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-05-03 03:43:54 +01:00
Enhance Groovy template support in CLI
to allow user to specify the TemplateEngine Fixes gh-908
This commit is contained in:
@@ -8,6 +8,7 @@ class Example implements CommandLineRunner {
|
||||
@Autowired
|
||||
private MyService myService
|
||||
|
||||
@Override
|
||||
void run(String... args) {
|
||||
print template("test.txt", ["message":myService.sayWorld()])
|
||||
}
|
||||
|
||||
+1
-1
@@ -51,7 +51,7 @@ public class SpringMvcCompilerAutoConfiguration extends CompilerAutoConfiguratio
|
||||
"org.springframework.web.servlet.config.annotation",
|
||||
"org.springframework.web.servlet",
|
||||
"org.springframework.web.servlet.handler", "org.springframework.http",
|
||||
"org.springframework.ui");
|
||||
"org.springframework.ui", "groovy.text");
|
||||
imports.addStaticImport(GroovyTemplate.class.getName(), "template");
|
||||
}
|
||||
|
||||
|
||||
@@ -16,11 +16,14 @@
|
||||
|
||||
package org.springframework.boot.groovy;
|
||||
|
||||
import groovy.lang.Writable;
|
||||
import groovy.text.GStringTemplateEngine;
|
||||
import groovy.text.Template;
|
||||
import groovy.text.TemplateEngine;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.net.URL;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
@@ -41,7 +44,15 @@ public abstract class GroovyTemplate {
|
||||
|
||||
public static String template(String name, Map<String, ?> model) throws IOException,
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user