如何避免在 Map 中模板缺少参数时出现 MissingPropertyException 并将未找到的值替换为 null?
import groovy.text.StreamingTemplateEngine
import groovy.text.Template
class Test {
private static Writable binding(Map map, String string) {
Template template = new StreamingTemplateEngine().createTemplate(string)
return template.make(map)
}
static void main(String... args) {
def template = "\${test1} \${test2}"
def map = ["test1": "test1"]
print binding(map, template)
}
}
Run Code Online (Sandbox Code Playgroud) groovy ×1