SpringBoot的Velocity模板文件

本文介绍了SpringBoot中使用Velocity模板文件的默认目录和文件格式,并提到了如何通过配置更改模板后缀。还探讨了Velocity模板的语法特点,以及其在实际应用中的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值