没有配置选项可以抑制此异常,但是您可以扩展传递给模板的映射并稍微更改其行为。考虑以下示例:
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
不见了)。
解决方案:为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))
Output:
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.
希望能帮助到你。