StreamingTemplateEngine 异常 MissingPropertyException

Sim*_*kin 5 groovy

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

Szy*_*iak 4

没有配置选项可以抑制此异常,但是您可以扩展传递给模板的映射并稍微更改其行为。考虑以下示例:

import groovy.text.StreamingTemplateEngine
import groovy.text.Template

def string = '''
Dear <% out.print firstname %> ${lastname},

We <% if (accepted) out.print 'are pleased' else out.print 'regret' %> 
to inform you that your paper entitled
'$title' was ${ accepted ? 'accepted' : 'rejected' }.

The conference committee.
'''

def map = [
    firstname: 'test',
    lastname: 'test',
    accepted: true
]

Template template = new StreamingTemplateEngine().createTemplate(string)
println template.make(map)
Run Code Online (Sandbox Code Playgroud)

它失败,但出现以下异常:

Caught: groovy.text.TemplateExecutionException: Template execution error at line 4:
         3:     We <% if (accepted) out.print 'are pleased' else out.print 'regret' %>     to inform you that your paper entitled
     --> 4:     '$title' was ${ accepted ? 'accepted' : 'rejected' }.
         5:     

groovy.text.TemplateExecutionException: Template execution error at line 4:
         3:     We <% if (accepted) out.print 'are pleased' else out.print 'regret' %>     to inform you that your paper entitled
     --> 4:     '$title' was ${ accepted ? 'accepted' : 'rejected' }.
         5:     

    at test.run(test.groovy:21)
Caused by: groovy.lang.MissingPropertyException: No such property: title for class: groovy.tmp.templates.StreamingTemplateScript1
    ... 1 more
Run Code Online (Sandbox Code Playgroud)

它失败是因为我们定义了 4 个模板变量中的 3 个(变量title丢失)。

解决方案:为a创建包装器Map

让我们解决它。我们将通过重写 map 方法来实现它containsKey(Object key),使其始终返回true(此方法由模板引擎使用,如果它返回false,模板引擎将引发异常)。我们将创建一个包装类,它封装一个映射并将不存在方法的调用委托给这个包装类。我们将这个类称为Bindings.

import groovy.text.StreamingTemplateEngine
import groovy.text.Template

class Bindings {
    @Delegate private final Map map

    Bindings(Map map) {
        this.map = map
    }

    boolean containsKey(Object key) {
        return true
    }
}

def string = '''
Dear <% out.print firstname %> ${lastname},

We <% if (accepted) out.print 'are pleased' else out.print 'regret' %> 
to inform you that your paper entitled
'$title' was ${ accepted ? 'accepted' : 'rejected' }.

The conference committee.
'''

def map = [
    firstname: 'test',
    lastname: 'test',
    accepted: true
]

Template template = new StreamingTemplateEngine().createTemplate(string)
println template.make(new Bindings(map))
Run Code Online (Sandbox Code Playgroud)

输出:

Dear test test,

We are pleased 
to inform you that your paper entitled
'null' was accepted.

The conference committee.
Run Code Online (Sandbox Code Playgroud)

已经没有MissingPropertyException抛出了。但是,正如您所看到的,null它被打印在null字符串内部。如果您想打印空字符串,您可以添加Object get(Object key)方法Bindings并覆盖其默认行为:

class Bindings {
    @Delegate private final Map map

    Bindings(Map map) {
        this.map = map
    }

    boolean containsKey(Object key) {
        return true
    }

    Object get(Object key) {
        return map.getOrDefault(key, '')
    }
}
Run Code Online (Sandbox Code Playgroud)

如果这样做,您将看到类似于以下内容的输出:

Dear test test,

We are pleased 
to inform you that your paper entitled
'' was accepted.

The conference committee.
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。