zoukankan      html  css  js  c++  java
  • Springboot(5)模板引擎

    1.thymeleaf

    (1)在pom.xml中引入thymeleaf;

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

    (2)如何关闭thymeleaf缓存

    ########################################################
    ###THYMELEAF (ThymeleafAutoConfiguration)
    ########################################################
    #spring.thymeleaf.prefix=classpath:/templates/
    #spring.thymeleaf.suffix=.html
    #spring.thymeleaf.mode=HTML5
    #spring.thymeleaf.encoding=UTF-8
    # ;charset=<encoding> is added
    #spring.thymeleaf.content-type=text/html
    # set to false for hot refresh
    spring.thymeleaf.cache=false

    (3)编写模板文件.html

    编写模板文件src/main/resouces/templates/hello.html:

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
    xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
    <title>Hello World!</title>
    </head>
    <body>
    <p th:text="${hello}"></p>
    </body>
    </html>  

    (4)编写访问模板文件controller

    @Controller public class TemplateController {

      @RequestMapping("/helloHtml")

       public String helloHtml(Map<String,Object> map){

        map.put("hello","from TemplateController.helloHtml");

        return "hello"; } }//返回模板页面名称

    2.freemarker

    <#if alist??><#list alist as b>${b.属性!''}</#list><#/if>

    <#list map?keys as key>${map[key]}   ${map.get(key)}</#list>

    <#assign num = 100 />

    ${date?string('yyyy-MM-dd')}

    (1)在pom.xml中引入freemarker;

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

    (2)如何关闭freemarker缓存

    spring.freemarker.cache=false

    #模板缓存延时
    spring.freemarker.settings.template_update_delay=0

    (3)编写模板文件.ftl

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
    xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
    <title>Hello World!</title>
    </head>
    <body>
    <p>${hello}</p>
    </body>
    </html>

    (4)编写访问文件并输出

    Configuration cfg = new Configuration(Configuration.getVersion());
    String classPath = this.getClass.getResource("/").getPath();
    cfg.setDirectoryForTemplateLoading(new File(classPath + "/templates"));
    Template template = cfg.getTemplate("test.ftl");
    String content = FreeMarkerTemplateUtils.processTemplateInfoString(template,map);//map为绑定数据
    InputStream inputStream = IOUtils.toInputStream(content);
    FileOutputStream outputStream = new FileOutputStream(new File("test.html"));

    常用内建函数:

           处理字符串:
                                 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   变成整数

            ${point?c}                                      point 变成字符串
                            处理list:
                                  first:                                              取List值第一个值
                                  last:                                                取List值最后一个值
                                  seq_contains:                                是否包含指定字符
                                  seq_index_of:                               指定字符所在位置
                                  size:                                                集合大小
                                   reverse:                                          集合倒序排列
                                  sort:                                                对集合进行排序
                                  sort_by:                                         根据某一个属性排序
                                  chunk:                                            分块处理
                            其他:
                                  is_string:                                      是否为字符类型
                                  is_number:                                    是否为整数类型
                                  is_method:                                   是否为方法
                                  ():                                                  判断整个变量
                                  has_content:                                判断对象是否为空或不存在
                                  text?eval:                                   text文本转json对象

  • 相关阅读:
    从一个程序的Bug解析C语言的类型转换
    Expression Blend使用笔刷
    Expression Blend入门
    C#生成CHM文件(中级篇)
    C#生成CHM文件(应用篇)
    C#创建不规则窗体的几种方式
    Web Service学习笔记(2)
    C#生成CHM文件(应用篇)之代码库编辑器(1)
    ASP.NET实际项目演练(1)
    Web Service学习笔记(4)
  • 原文地址:https://www.cnblogs.com/god-monk/p/8856037.html
Copyright © 2011-2022 走看看