<!-- 获取变量的值 --> ${name1}、${name2}、${name3} 嵌套页面 <#include "he..."/>

小太阳的博客

FreeMarker

FreeMarker常用语法

定义变量

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

嵌套页面

<#include "header.ftl">

if else

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

if-else
<#if success==true>
	// 执行
<#else>
	// 执行
</#if>

if-else if-else
<#if condition>
	// 执行
<#elseif condition2>
	// 执行
<#elseif condition3>
	// 执行
<#else>
	// 执行
</#if>

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

<#list list as entity>
	// 下标:${entity_index} 或 ${entity_index+1}
	// 值:${entity}
</#list>

<#list list as entity>
	// 下标:${entity_index} 或 ${entity_index+1}
	// 值:${entity}
<#else>
	// 没有数据
</#list>
一共${list?size}条记录

判断变量是否存在

<#if aaa??>
	// aaa变量存在${aaa}
<#else>
	// aaa变量不存在
</#if>

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

${bbb!''}

freemarker运算符

<#if score gte 60>
	及格
<#elseif score gte 80 && score lte 90>
	良好
<#else>
	高材生
</#if>

FreeMarker的内建函数

<#--将现有时间以时间的格式显示,显示结果如:15:13:05-->
${nowDate?time}

<#--以日期格式显示,如:2011-4-28-->(date的格式可以在freemarker.properties文件中配置)
${nowDate?date}

日期格式化
${time?string("yyyy-MM-dd")}

截取字符串
${str?substring(0,2)}

indexof的使用
${str?last_index_of(",")}

split的使用
<#list "12,13,14,15"?split(",") as item>
	${item}
</#list>

日期格式转换
<#if entity.createTime??>${entity.createTime?string('yyyy-MM-dd HH:mm:ss')}</#if>

对null值得处理

!对输出的空值做处理,只输出无返回值
输出name的值:${name}。如果name为null,就会报错
输出name的值:${name!}。如果name为null,就不会报错,什么也没输出(重点)
输出name的值:${name!"默认值"}。如果name为null,就输出”默认值”字符串(重点)
输出name的值:${name!100}。如果name为null,就输出100
输出user.name的值:${(user.name)!"默认值”},如果user或name为null,就输出默认值(重点)
输出user.name的值:${user.name!"默认值”},如果user为null会报错,如果name为null,就输出默认值
使用default内建函数来处理:${user.name?default('vakin')}(较繁琐)

??测试是否为null,返回boolean值
product.color??将只测试color是否为null
(product.color)??将测试product和color是否存在null

freemarker中??和?的区别
??是判断对象是否为空,例如:<#if object??>object对象不为空(即object存在)</#if>
?后面要加内建函数名,例如:<#if object?exists>object对象不为空(即object存在)</#if>
<#if str??>${str?string}</#if><#--将str以字符串形式显示-->
<#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页面

Copyright © 2023,版权所有 - 小太阳的博客 - 黑ICP备2023000004号