如何避免在 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)
}
}
最佳答案
没有配置选项来抑制此异常,但是您可以扩展您传递给模板的映射并稍微更改其行为。考虑以下示例:
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)
它失败,但有以下异常:
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
它失败了,因为我们从 4 个模板变量中定义了 3 个(变量
title
缺失)。解决方案:为
Map
创建包装器让我们修复它。我们将通过覆盖 map 方法
containsKey(Object key)
来做到这一点在某种程度上它总是返回 true
(此方法由模板引擎使用,如果返回 false
,则模板引擎抛出异常)。我们将创建一个封装类,该类封装一个映射并将不存在的方法的调用委托(delegate)给该封装类。我们将调用这个类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))
输出:
Dear test test,
We are pleased
to inform you that your paper entitled
'null' was accepted.
The conference committee.
没有
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, '')
}
}
如果这样做,您将看到类似于以下内容的输出:
Dear test test,
We are pleased
to inform you that your paper entitled
'' was accepted.
The conference committee.
希望能帮助到你。
关于groovy - StreamingTemplateEngine 异常 MissingPropertyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50335047/