Groovy中String转换Gstring用于动态插值
2024-09-04 14:16:57
知识点是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()
最新文章
- 转:隐马尔可夫模型(HMM)攻略
- mybatis入门基础(二)----原始dao的开发和mapper代理开发
- Script 简单语句的练习题
- [2015hdu多校联赛补题]hdu5372 Segment Game
- TYVJ P1026 犁田机器人 Label:水
- c语言typedef的用法-解惑阿!很多天书般的东西解释的不错(转)
- EASYUI Dialog的基本使用
- http_build_query函数(学习)
- Entity Framework系列文章导航
- linux下服务器管理
- POJ Octal Fractions(JAVA水过)
- 深入理解Java虚拟机到底是什么
- [SHOI2014]三叉神经树
- OpenResty的现状、趋势、使用及学习方法
- centos7下kubernetes(10。kubernetes-daemonset)
- mybatis理解(0)
- dev16 cxgrid 在DLL里报0地址错
- linux 测试网络延迟
- JedisCluster中应用的Apache Commons Pool对象池技术
- spring jdbc批量插入