知识点是Groovy中的模板引擎
GStringTemplateEngine
第一个例子:
def binding = [ firstname : "Grace", lastname : "Hopper", accepted : true, title : 'Groovy for COBOL programmers' ] def engine = new groovy.text.GStringTemplateEngine() def text = '''\ Dear <%= firstname %> $lastname, We <% if (accepted) print 'are pleased' else print 'regret' %> \ to inform you that your paper entitled '$title' was ${ accepted ? 'accepted' : 'rejected' }. The conference committee. ''' def template = engine.createTemplate(text).make(binding) println template.toString()
第二个例子:
String stringFromDatabase = 'Hello ${name}!'String name = 'world'def engine = new groovy.text.SimpleTemplateEngine()assert 'Hello world!'== engine.createTemplate(stringFromDatabase).make([name:name]).toString()