Groovy StreamingTemplateEngine异常缺少PropertyException
如何避免在映射中缺少模板参数时出现MissingPropertyException,并用null替换未找到的值Groovy StreamingTemplateEngine异常缺少PropertyException,groovy,Groovy,如何避免在映射中缺少模板参数时出现MissingPropertyException,并用null替换未找到的值 import groovy.text.StreamingTemplateEngine import groovy.text.Template class Test { private static Writable binding(Map map, String string) { Template template = new StreamingTempl
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)
输出:
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方法: 作为旁注,我写了一篇文章,作为对几年前其他模板引擎局限性的贡献和回应。很高兴看到它被使用
尽管事后看来这还远远不够完美。用一种更好的内部方法写另一本已经在我的清单上很久了 您应该提供一个可复制的示例。@MarmiteBomber您是对的,我添加了一个示例谢谢,地图包装器帮助了我哇,它帮助了我,节省了我很多时间,谢谢有没有办法保留默认值功能,如${myAbsentKey?:'defaultValue'}?因为使用此解决方案时,如果密钥不存在,则总是将其设置为null:'(
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.
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
].withDefault { "<not found>" }
Template template = new StreamingTemplateEngine().createTemplate(string)
println template.make(map)
~> groovy test.groovy
Dear test test,
We are pleased
to inform you that your paper entitled
'<not found>' was accepted.
The conference committee.