SpringBoot微框架的velocity模板文件默认目录:templates,文件名为xxx.vm,可以在application.properties文件修改模板的后缀,如spring.velocity.suffix=.html,默认为.vm
Velocity模板语法(类似Java语法)
$!{ 变量/表达式 }
## 注释 ## #* 多行注释 *#
for
#foreach ($color in $colors)
Color$!{foreach.count}/${foreach.index}:$!{color}
#end
实例应用:
<pre>
#*
块注释
*#
属性访问
$!{value1}
$!{value2} ##!如果不存在,强制为空
${value3} ##没有!表示如果不存在,则按文本输出
$!{colors}
#foreach($color in $colors)
This is color $!{foreach.index}:$color $!{foreach.count}
#end
循环Map类型的数据:
#foreach($key in $map.keySet())
Number:$key $map.get($key)
#end
#foreach($kv in $map.entrySet())
Number:$kv.key $kv.value ##kv.key == kv.getKey() kv.value == kv.getValue()
#end
User: $!{user.name}
$!{user.description}
$!{user.getDescription()}
#set($title = "nowcoder_test")
Title:$!{title}
模板继承
Parse: #parse("header.html") ##解析header.html
Include: #include("header.html") ##只是把header.html包含进来,纯文本扩展
定义宏:
#macro(render_color,$index,$color) ##定义一个宏
Color Render Macro test $index,$color
#end
#foreach($color in $colors)
#render_color($foreach.index,$color)
#end
设置变量:
#set($hello = "hello")
#set($helloworld1 = "$!{hello} world")
$!{hello}$hello $!hello ##获取变量的值,(1)$hello,(2)$!hello,(3)$!{hello}
test:$helloworld1
</pre>