FreeMarker常用语法

定义变量

  1. <!-- 声明变量 -->
  2. <#assign name1="刘备" name2="关羽" name3="张飞">
  3. <!-- 获取变量的值 -->
  4. ${name1}、${name2}、${name3}

嵌套页面

  1. <#include "header.ftl">

if

  1. <#if success==true>// 执行</#if>

if-else

  1. <#if success==true>
  2. // 执行
  3. <#else>
  4. // 执行
  5. </#if>

if-else if-else

  1. <#if condition>
  2. // 执行
  3. <#elseif condition2>
  4. // 执行
  5. <#elseif condition3>
  6. // 执行
  7. <#else>
  8. // 执行
  9. </#if>

list遍历循环(注意:list在前)

  1. <#list list as entity>
  2. // 下标:${entity_index} 或 ${entity_index+1}
  3. // 值:${entity}
  4. </#list>
  5. <#list list as entity>
  6. // 下标:${entity_index} 或 ${entity_index+1}
  7. // 值:${entity}
  8. <#else>
  9. // 没有数据
  10. </#list>
  11. 一共${list?size}条记录

判断变量是否存在

  1. <#if aaa??>
  2. // aaa变量存在${aaa}
  3. <#else>
  4. // aaa变量不存在
  5. </#if>

三目运算符:变量bbb为空时,返回空字符串

  1. ${bbb!''}

freemarker运算符

  1. <#if score gte 60>
  2. 及格
  3. <#elseif score gte 80 && score lte 90>
  4. 良好
  5. <#else>
  6. 高材生
  7. </#if>

FreeMarker的内建函数

  1. <#--将现有时间以时间的格式显示,显示结果如:15:13:05-->
  2. ${nowDate?time}
  3. <#--以日期格式显示,如:2011-4-28-->(date的格式可以在freemarker.properties文件中配置)
  4. ${nowDate?date}
  5. 日期格式化
  6. ${time?string("yyyy-MM-dd")}
  7. 截取字符串
  8. ${str?substring(0,2)}
  9. indexof的使用
  10. ${str?last_index_of(",")}
  11. split的使用
  12. <#list "12,13,14,15"?split(",") as item>
  13. ${item}
  14. </#list>
  15. 日期格式转换
  16. <#if entity.createTime??>${entity.createTime?string('yyyy-MM-dd HH:mm:ss')}</#if>

对null值得处理

  1. !对输出的空值做处理,只输出无返回值
  2. 输出name的值:${name}。如果namenull,就会报错
  3. 输出name的值:${name!}。如果namenull,就不会报错,什么也没输出(重点)
  4. 输出name的值:${name!"默认值"}。如果namenull,就输出”默认值”字符串(重点)
  5. 输出name的值:${name!100}。如果namenull,就输出100
  6. 输出user.name的值:${(user.name)!"默认值”},如果usernamenull,就输出默认值(重点)
  7. 输出user.name的值:${user.name!"默认值”},如果usernull会报错,如果namenull,就输出默认值
  8. 使用default内建函数来处理:${user.name?default('vakin')}(较繁琐)
  9. ??测试是否为null,返回boolean
  10. product.color??将只测试color是否为null
  11. (product.color)??将测试productcolor是否存在null
  12. freemarker中??和?的区别
  13. ??是判断对象是否为空,例如:<#if object??>object对象不为空(即object存在)</#if>
  14. ?后面要加内建函数名,例如:<#if object?exists>object对象不为空(即object存在)</#if>
  15. <#if str??>${str?string}</#if><#--将str以字符串形式显示-->
  16. <#if (gzTableInfo.remark)??>我是来测试的</#if>

获取session中保存的值

后台servlet中:
request.getSession().setAttribute(sessionName, obj);

Freemarker中:
<#if Session["aa"]?exists>
    ${Session["aa"]}
</#if>

<#if Session["userInfo"]?exists>
    ${Session['userInfo'].email}
</#if>

常用示例

<table id="table" class="table">
    <thead>
        <tr>
            <th class="checkbox"><input type="checkbox" class="fill listen-1" /> </th>
            <th style="wid80px;">序号</th>
            <th>表名</th>
            <th>表述</th>
            <th>创建时间</th>
            <th>最后修改时间</th>
            <th>备注</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        <#list pageInfo.list as entity>
            <tr>
                <td class="checkbox"><input type="checkbox" class="fill listen-1-2" value="${entity.id}" name="id"/> </td>
                <td>${entity_index+1}</td>
                <td>${entity.tableName}</td>
                <td>${entity.chsname}</td>
                <td><#if entity.createTime??>${entity.createTime?string('yyyy-MM-dd HH:mm:ss')}</#if></td>
                <td><#if entity.updateTime??>${entity.updateTime?string('yyyy-MM-dd HH:mm:ss')}</#if></td>
                <td>${entity.remark}</td>
                <td>
                    <#if entity.status==0>
                        <font color="green">启用</font>
                    <#else>
                        <font color="red">废弃</font>
                    </#if>
                </td>
                <td>
                    <button class="button red radius-3" onclick="del(this, '${entity.id}')"><span class="icon-trash-o"></span> 删除</button>
                    <button class="button indigo radius-3" onclick="edit('${entity.id}')"><span class="icon-edit-2"></span> 编辑</button>
                    <a class="button green radius-3" href="/table_column_info/list?tableId=${entity.id}"><span class="icon-grid"></span> 字段管理</a>
                </td>
            </tr>
        <#else>
            <tr><td colspan="9" style="text-align:center;">暂无记录</td></tr>
        </#list>
    </tbody>
</table>

springboot配置FreeMarker

pom依赖

<!-- 引入freeMarker的依赖包 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

application.yml文件

spring:
    freemarker:
        request-context-attribute: req
    suffix: .html
    content-type: text/html
    enabled: true
    cache: false
    template-loader-path: classpath:/templates/
    charset: UTF-8
    prefer-file-system-access: false
    #设置将null置为空字符串
    settings:
        classicCompatible: true
    mvc:
      static-path-pattern: /**

文件位置

controller正常写就行
@RequestMapping("/index")
public String toIndex(Model model) {
    model.addAttribute("name", "lisi");
    return "index";
}

static文件夹里存放静态文件,如css,js等
templates文件夹里存放html页面