springboot~使用freemaker模版进行部署

实事上keycloak框架使用了freemaker进行页面部署,在页面上使用了vue进行了渲染,还是比较跟的上技术前沿的,只不过,keycloak没有使用spring框架,可能是因为它是redhat公司推出的产品吧。

依赖引用

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- FreeeMarker模板引擎所需依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

配置文件

spring:
  freemarker:
    suffix: .ftl                                 # 设置模板后缀名
    content-type: text/html                      # 设置文档类型
    charset: UTF-8                               # 设置页面编码格式
    cache: false                                 # 设置页面缓存
    template-loader-path: classpath:/templates   # 设置ftl文件路径
  mvc:
    static-path-pattern: /static         # 设置静态文件路径,js,css等

添加模板

  • resources/templates/template.ftl
<#macro registrationLayout bodyClass="默认值,子页面没有设置会使用这个值,子页面设置后会覆盖它">
<#--语法介绍:-->
<#--<#macro name param1 param2 ... paramN>--><!-- macro在模板里定义的变量,在子页面中可以重写变量的内容,它会反映的模板里-->
<#--<#nested loopvar1, loopvar2, ..., loopvarN>-->
<#--<p>${param1?cap_first}-->
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title><#nested "head"></title>
    </head>
    <body>
    <p>
        ${bodyClass}
    </p>
    <h1>FreeMaker模板页面</h1>
    <#nested "form">
    </body>
    </html>
</#macro>

添加页面

  • resources/templates/f1/list.ftl
<#import "../template.ftl" as layout>
<@layout.registrationLayout bodyClass="<span style='color:red'>修改模板里的变量</span>";section>
    <#if section = "head">
        列表
    <#elseif section = "form">
        <table border="1">
            <tr>
                <td>编号</td>
                <td>名称</td>
                <td>年龄</td>
                <td>操作</td>
            </tr>
        </table>
    </#if>
</@layout.registrationLayout>

添加controller

@Controller
@RequestMapping("free")
public class F1Controller {
    @RequestMapping("list")
    public String selectUser(){
        return "f1/list";
    }
}

上面用到了freemaker中的宏macro,引用import,模块嵌入nested 首字母大写内建函数?cap_first等知识点,大家如果看不懂,可以查以上相关资料。

页面运行后的截图

freemaker几个内建函数

(1) 常用内建函数

  • 处理字符串:
    • ?substring 截取字符串,包头不包尾(下标)
    • ?cap_first 第一个字母大写
    • ?end_with 以什么字母结尾
    • ?contains 是否包含目标字符串
    • ?date datetime time 转换成日期格式
    • ?starts_with 以什么字母开头
    • ?index_of 返回某个指定的字符串值在字符串中首次出现的位置(下标)
    • ?last_index_of 获取指定字符出现的最后位置(下标)
    • ?split 分隔
    • ?trim 去两端空格
  • 处理数字:
    • ?string x?string(“0.##”) 变成小数点后几位
    • ?round 四舍五入
    • ?floor 去掉小数点
    • ?ceiling 近1 变成整数
  • 处理list:
    • ?first: 取List值第一个值
    • ?last: 取List值最后一个值
    • ?seq_contains: 是否包含指定字符
    • ?seq_index_of: 指定字符所在位置
    • ?size: 集合大小
    • ?reverse: 集合倒序排列
    • ?sort: 对集合进行排序
    • ?sort_by: 根据某一个属性排序
    • ?chunk: 分块处理
  • 其他:
    • ?is_string: 是否为字符类型
    • ?is_number: 是否为整数类型
    • ?is_method: 是否为方法
  • 判断整个变量
    • ?has_content: 判断对象是否为空或不存在
    • ?eval: 求值

(2) macro(宏指令)

  • 调用:<@macro_name param />
<#macro 变量名 参数>
  <#nested/>
</#macro>

(3) function(函数指令)

  • 调用:$
<#function 变量名 参数>
  <#return>
</#function>
posted @ 2021-08-05 09:43  张占岭  阅读(296)  评论(0编辑  收藏  举报