zoukankan      html  css  js  c++  java
  • Spring Boot----freemark使用

    项目创建

      1、添加依赖

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
            </dependency>
    

      2、配置(测试环境中需要添加)

    spring:
      application:
        name: test-freemarker #指定服务名
      freemarker:
        cache: false  #关闭模板缓存,方便测试
        settings:
          template_update_delay: 0 #检查模板更新延迟时间,设置为0表示立即检查,如果时间大于0会有缓存不方便进行模板测试
    

    使用

      在resource目录下创建templates目录,freemark默认从这里面找所有的xx.ftl文件

    ftl文件

    <table>
        <tr>
            <td>姓名</td>
            <td>年龄</td>
            <td>出生日期</td>
            <td>钱包</td>
            <td>最好的朋友</td>
            <td>朋友个数</td>
            <td>朋友列表</td>
        </tr>
        <#if stus??>
        <#list stus as stu>
            <tr>
                <td>${stu.name!''}</td>
                <td>${stu.age}</td>
                <td>${(stu.birthday?date)!''}</td>
                <td>${stu.mondy}</td>
                <td>${(stu.bestFriend.name)!''}</td>
                <td>${(stu.friends?size)!0}</td>
                <td>
                    <#if stu.friends??>
                    <#list stu.friends as firend>
                        ${firend.name!''}<br/>
                    </#list>
                    </#if>
                </td>
            </tr>
        </#list>
        </#if>
    
    </table>
    <br/>
    <#assign text="{'bank':'工商银行','account':'10101920201920212'}" />
    <#assign data=text?eval />
    开户行:${data.bank}  账号:${data.account}
    
    </body>
    </html>
    

      

    语法

    ftl文件的注释

    <#--xx-->
    

    擦值表达式

    Hello ${name}!

    遍历List

    <table>
        <tr>
            <td>序号</td>
            <td>姓名</td>
            <td>年龄</td>
            <td>钱包</td>
        </tr>
        <#list stus as stu>
            <tr>
                <td>${stu_index + 1}</td>
                <td <#if stu.name =='小明'>style="background:red;"</#if>>${stu.name}</td>
                <td>${stu.age}</td>
                <td >${stu.mondy}</td>
            </tr>
        </#list>
    </table>
    

    遍历HashMap

    遍历HashMap方式1:<br/>
    姓名:${stuMap['stu1'].name}<br/>
    遍历HashMap方式2:<br/>
    姓名:${stuMap.stu1.name}<br/>
    遍历HashMap方式3:<br/>
    <table>
        <tr>
            <td>序号</td>
            <td>姓名</td>
            <td>年龄</td>
            <td>钱包</td>
        </tr>
      <#list stuMap?keys as k>
      <tr>
          <td>${k_index + 1}</td>
          <td>${stuMap[k].name}</td> //注意此时不能使用${stuMap.k.name}
          <td>${stuMap[k].age}</td>
          <td >${stuMap[k].mondy}</td>
      </tr>
      </#list>
    </table>
    

    if指令

    <td <#if stu.nane=='小明'> style="background:cornfloverblue;" </#if>>${stu.nane}</td>
    

      同

    <#if stu.nane == '小明'>
        <td  style="background:cornfloverblue;">${stu.nane}</td>
    </#if>
    <#if stu.nane != '小明'>
        <td>${stu.nane}</td>
    </#if>
    

    算术符号

      <#if a>1></#if>,不能使用‘>’,使用gt

      <#if (a>1)></#if>,或者这样使用

    逻辑与:&&
    逻辑或: ||
    逻辑非:!

    空值处理

    <#if stus??></#if> :如果stus不为空
    

      缺省值${()!''},只要括号中有一个属性为空,那么直接返回‘’

    ${(stuMap.stu.name)!''} //如果stuMap.stu为null,如果不设置默认值直接报错,此时如果为null,直接返回''
    

    内建函数

      1、得到某个集合的大小

    ${集合?size}
    

      2、格式化时间

    显示年月日:${today?date}
    显示时分秒:${today?time)
    显示年月日时分秒:${today?datetime}<br>
    自定义格式化:${today?string("yyyy年w月")}
    

      3、数字

    如果Model添加了一个数字
    Model.addtribute("num",123456789)
    
    此时
    ${num}--->123,456,789
    如果需要直接显示123456789
    ${num?c}
    

      4、导入一个new.ftl,和在new.ftl使用变量currentNav

    		<#assign currentNav="personal" />
    		<#include "common/navbar-tpl.ftl" />
    

      

    <#if currentNav??>
    <script type="text/javascript">
    	$("#"+"${currentNav}").addClass("active");
    </script>
    </#if>
    

      

    文件静态化

      1、基于本地文件

        public void test() throws IOException, TemplateException {
            //定义配置类
            Configuration configuration = new Configuration(Configuration.getVersion());
            //得到classpath的路径
            String classpath=this.getClass().getResource("/").getPath();
            //定义模板路径(template存放着ftl文件)
            configuration.setDirectoryForTemplateLoading(new File(classpath+"/template/"));
            //获取模板文件内容
            Template template = configuration.getTemplate("test.ftl");
            //定义数据模型
            HashMap<String, String> map = new HashMap<>();
            map.put("user","小明");
    
            //此时string就是一个html文件
            String string = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
    
            //写出文件
            FileUtils.writeStringToFile(new File("xx"),string,"utf-8");
        }
    

      2、基于网络数据字符串

        public void test() throws IOException, TemplateException {
            //定义配置类
            Configuration configuration = new Configuration(Configuration.getVersion());
            //模板内容
            String templateString="" +
                    "<html>
    " +
                    "    <head><meta charset="UTF-8"></head>
    " +
                    "    <body>
    " +
                    "    名称:${user}
    " +
                    "    </body>
    " +
                    "</html>";
    
            //加载模板
            //模板加载器
            StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
            stringTemplateLoader.putTemplate("template",templateString);
            configuration.setTemplateLoader(stringTemplateLoader);
            Template template = configuration.getTemplate("template","utf-8");
    
            //定义数据模型
            HashMap<String, String> map = new HashMap<>();
            map.put("user","小明");
    
            //此时string就是一个html文件
            String string = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
    
            //写出文件
            FileUtils.writeStringToFile(new File("xx.html"),string,"utf-8");
        }
  • 相关阅读:
    D3js-实现图形拖拽及双击恢复
    D3js-三种图表tooltip提示框总结介绍
    D3js-API介绍【英】
    D3js-API介绍【中】
    springboot 整合 activemq 同时使用点对点模式和发布订阅模式
    docker 安装 activemq
    nginx 配置websocket 400 问题
    springboot +vue 整合websocket 403
    m3u8下载器
    linux scp 命令
  • 原文地址:https://www.cnblogs.com/yanxiaoge/p/11826435.html
Copyright © 2011-2022 走看看