小编Sim*_*kin的帖子

StreamingTemplateEngine 异常 MissingPropertyException

如何避免在 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

5
推荐指数
1
解决办法
1208
查看次数

标签 统计

groovy ×1