博客
关于我
FreeMarker开发总结
阅读量:557 次
发布时间:2019-03-09

本文共 1703 字,大约阅读时间需要 5 分钟。

FreeMarker总结

1.FreeMarker的定义

FreeMarker是一个基于java的免费开源的模板引擎,基于文本的模板输出工具,有着丰富的表达式,还可以自定义表达式。Freemarker可以将后端准备好的数据,通过Freemarker表达式在前端页面展示出来。在软件项目中常常用来生成HTML的 Web页面,可以基于模板生成输出文件,导出复杂样式的excel。

2.FreeMarker工作原理图

在这里插入图片描述

3.springboot工程中配置

  1. 添加maven依赖
org.springframework.boot
spring-boot-starter-freemarker
  1. 在springboot项目中的yml配置
spring:  freemarker:    cache: false    suffix: .ftl    template-loader-path: classpath:/templates    content-type: text/html; charset=utf-8    settings:      number_format: 0   # 数字不进行千分位自动转换,比较大的数字会带有逗号,如1,000

4.常用方法

在说方法前,先吐槽一下Freemarker比较恶心的地方,就是空错误,如果你页面表达式是null,就会报错,所以在使用的时候要先进行空判断,有时候页面报错就是因为这个。

1.获取元素值

<#if (msg)!="">     ${msg}

2.获取map中的值

${map["name"]}

3.foreach,循环输出list,先判断是否

<#if students ?? && students ?size gt 0>	   <#list students as student>		    ${student.name}		    ${student.age}		    ${student.sex}	   

4.逻辑判断,if…else方法,

字符串类型判断

<#if type == 1>    type=1                                 <#elseif type == 2>    type=2

布尔值类型判断

<#if (booleanVal?string('yes', 'no'))=='yes'>   <#else>   

多个值进行if判断

<#if flag=="1">	 1 <#elseif flag=="2">	 2 <#elseif flag=="3">	 3 <#else> 	 除了1、2、3之外 

5.获取session中的值,如后端在session中保存userId,使用Freemarker

在session中保存userId的值

request.getSession().setAttribute("userId",userId);

在页面使用freemarker表达式,为避免userId的值是空就会报错,所以要先判断一下

<#if Session["userId"]?exists> 	${Session["userId"]} 

6.Freemarker格式化时间

// 输出时间格式 2020-05-09 13:59:32${createTime?string('yyyy-MM-dd hh:mm:ss')}

7.使用include标签,引入其他页面

抽出公共页面代码,取名right.ftl,如下

在需要引入该代码的页面中,使用include标签

<#include "right.ftl">

转载地址:http://btdpz.baihongyu.com/

你可能感兴趣的文章
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
查看>>
mysql csv import meets charset
查看>>
multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
查看>>
MySQL DBA 数据库优化策略
查看>>
multi_index_container
查看>>
MySQL DBA 进阶知识详解
查看>>
Mura CMS processAsyncObject SQL注入漏洞复现(CVE-2024-32640)
查看>>
Mysql DBA 高级运维学习之路-DQL语句之select知识讲解
查看>>
mysql deadlock found when trying to get lock暴力解决
查看>>
MuseTalk如何生成高质量视频(使用技巧)
查看>>
mutiplemap 总结
查看>>
MySQL DELETE 表别名问题
查看>>
MySQL Error Handling in Stored Procedures---转载
查看>>
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>