• Java开发的模板引擎--freemarker


    一、基础知识

    1.1 模版引擎

    一种基于模版和要改变的数据,并用来生成输出文本(html、电子邮件、配置文件,源代码)的通用工具。他不是面向最终用户的,而是一个Java类库;
    
    • 1

    1.2 关于freemarker

    freemarker是免费的,其模版编写为freemarker template language(FTL freemarker模版语言),属于简单的专用语言。需要准备数据在起真实编程语言中显示,例如数据库查询和业务运算、之后模版显示已经准备好的数据;而在模版中主要用户如何展现数据;
    也就是说:模版(template)+数据(java object) ==freemarker==》作用下成为要的output
    
    • 1
    • 2

    1.3 常用的java模版引擎

    Jsp、freemarker、(Thymeleaf 、Velocity )
    
    • 1

    1.4 快速入门

    1.4.1 指定了SpringBoot的版本

        <parent>
           <groupId>org.springframework.bootgroupId>
           <artifactId>spring‐boot‐starter‐parentartifactId>
           <version>2.1.3.RELEASEversion>
    parent>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1.4.2 指定了pom.xml文件依赖

    <dependencies>
           <dependency>
               <groupId>org.springframework.bootgroupId>
               <artifactId>spring‐boot‐starter‐freemarkerartifactId>
           dependency>
           <dependency>
               <groupId>org.springframework.bootgroupId>
               <artifactId>spring‐boot‐starter‐webartifactId>
           dependency>
           <dependency>
               <groupId>org.projectlombokgroupId>
               <artifactId>lombokartifactId>
           dependency>
           <dependency>
               <groupId>com.squareup.okhttp3groupId>
               <artifactId>okhttpartifactId>
           dependency>
           <dependency>
               <groupId>org.springframework.bootgroupId>
               <artifactId>spring‐boot‐starter‐testartifactId>
           dependency>
           <dependency>
               <groupId>org.apache.commonsgroupId>
               <artifactId>commons‐ioartifactId>
           dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    1.4.2 指定了配置文件application.yml

    server:
    	port: 8088 #服务端口 
    	servlet:
           context‐path: /test‐freemarker
    spring:
        application:
    		name: test‐freemarker #指定服务名 
    	freemarker:
            charset: UTF‐8
            content‐type: text/html
            suffix: .ftl
            enabled: true
            # 模版的加载路径
            template‐loader‐path: classpath:/templates/
    	resources:
    		add‐mappings: false #关闭工程中默认的资源处理
    	mvc:
    		throw‐exception‐if‐no‐handler‐found: true #出现错误时直接抛出异常
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    二、FreeMarker的基础指令

    2.1 数据模型

    Freemarker静态化依赖数据模型和模板,一般在方法中的形参map即为freemarker静态化所需要的数据模型,在map中填充数据;
    
    • 1

    注意:关于freemarker的指令需要知道

    1. 注释,即<#‐‐和‐‐>,介于其之间的内容会被freemarker忽略
    2. 插值(Interpolation):即 . . 部分 , f r e e m a r k e r 会用真实的值代替 {..}部分,freemarker会用真实的值代替 ..部分,freemarker会用真实的值代替{…}
    3. FTL指令:和HTML标记类似,名字前加#予以区分,Freemarker会解析标签中的表达式或逻辑。
    4. 文本,仅文本信息,这些不是freemarker的注释、插值、FTL指令的内容会被freemarker忽略解析,直接输出内 容。

    2.2 关于数据模型map类型为中为List的指令

    2.2.1 遍历数据模型map类型为中为list

    <#list stus as stu>
    
    
    
    
    • 1
    • 2
    • 3
    • 4

    2.2.1 取一个对象中的各个属性

    			<tr>
    				
                   <td>${stu_index + 1}td>
                   
                   <td>${stu.name}td>
                   <td>${stu.age}td>
                   <td>${stu.mondy}td>
               tr>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.3 关于数据模型map类型为中为map的指令

    2.3.1 关于获取数据类型map中类型为map中的莫一键的值

    
    
    
    ${stuMap['stu1'].name}<br/>
    
    
    
    ${stuMap.stu1.name}<br/>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.3.2 遍历数据模型map类型为中为map

    
    
    
    <#list stuMap?keys as k>
    	<tr>
    		
           <td>${k_index + 1}td>
           
           <td>${stuMap[k].name}td>
           <td>${stuMap[k].age}td>
           <td >${stuMap[k].mondy}td>
       tr>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.4 部分操作指令

    2.4.1 if指令

    if 指令即判断指令,是常用的FTL指令,freemarker在解析时遇到if会进行判断,条件为真则输出if中间的内容,否则跳过内容不再输出。
    demo:
    
    • 1
    • 2
    
    
    <#if stu.name =='小明'>
    
    • 1
    • 2
    • 3

    2.4.2 运算符指令

    1. 算数运算符 FreeMarker表达式中完全支持算术运算,FreeMarker支持的算术运算符包括:+, - , * , / , %
    2. 逻辑 运算符 逻辑运算符有如下几个: 逻辑与:&& 逻辑或:|| 逻辑非:! 逻辑运算符只能作用于布尔值,否则将产生错误
    3. 比较运算符 表达式中支持的比较运算符有如下几个:
      1. =或者==:判断两个值是否相等.
      2. !=:判断两个值是否不等.
      3. 或者gt:判断左边值是否大于右边值

      4. =或者gte:判断左边值是否大于等于右边值

      5. <或者lt:判断左边值是否小于右 边值
      6. <=或者lte:判断左边值是否小于等于右边值
        关于运算符的注意点
        注意: =和!=:可以用于字符串,数值和日期来比较是否相等,但**=和!=两边必须是相同类型的值**,否则会产生错误,而且 FreeMarker是精确比较,“x”,"x ","X"是不等的.其它的运行符可以作用于数字和日期,但不能作用于字符串,大部分的时 候,使用gt等字母运算符代替>会有更好的效果,因为 FreeMarker会把**>解释成FTL标签的结束字符**,当然,也可以使用括 号来避免这种情况,如:<#if (x>y)>

    2.4.2 null值处理指令

    1. 判断某变量是否存在使用 “??” 用法为:variable??,如果该变量存在,返回true,否则返回false
      demo: <#if stus??>
    2. 缺失变量默认值使用 “!” 使用!要以指定一个默认值,当变量为空时显示默认值
      demo:${name!‘’}表示如果name为空显示空字符串
      如果是嵌套对象则建议使用()括起来。
      例: ${(stu.bestFriend.name)!‘’}表示,如果stu或bestFriend或name为空默认显示空字符串。

    2.5 关于FreeMarker的内置对象

    在freemarker中可以访问Request、Session等内置对象
    Request: 用于获取Request对象中的attribute对象。
    Session:用于获取Session对象中的attribute对象。
    RequestParameters:用于获取Request对象的parameter参数(浏览器端发送的请求数据)
    注意
    使用内置对象需要先暴露内置对象==》通过配置文件

    spring:
    	freemarker:
    		request‐context‐attribute: rc #把Spring的RequestContext对象暴露为变量rc 
    		expose‐request‐attributes: true
    		expose‐session‐attributes: true
    
    • 1
    • 2
    • 3
    • 4
    • 5

    三级目录

  • 相关阅读:
    php生成二维码合成文字、背景图并保存本地图片
    基于微信小程序的电影院订票选座系统的设计与实现(2)
    精心整理了超详细的Linux入门笔记,零基础也能看懂,一学就会
    探索增强型灰狼优化算法
    ZooKeeper之Java API的基本使用以及应用场景的实现
    numpy学习笔记
    特斯拉强制返岗遭亚马逊微软挖人:“不喜欢马斯克的速来,我们可居家办公”
    P1952 火星上的加法运算
    Layui合计自定义列
    如何修改NuGet默认全局包文件夹的位置?
  • 原文地址:https://blog.csdn.net/ljbnb/article/details/126353970