支持的包
<dependencies> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.29</version> </dependency> </dependencies>
springMVC-servlet.xml
<!-- 设置freeMarker的配置文件路径 -->
<bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:/properties/freemarker.properties"/>
</bean>
<!-- 配置freeMarker的模板路径 -->
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!-- 编码设置 -->
<property name="freemarkerSettings" ref="freemarkerConfiguration"/>
<property name="templateLoaderPath">
<value>/WEB-INF/ftl/</value>
</property>
</bean>
<bean id="freeMarkerViewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="prefix" value="" />
<property name="contentType" value="text/html; charset=UTF-8" />
</bean>
<!-- 配置freeMarker视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
<property name="contentType" value="text/html; charset=utf-8"/>
<property name="cache" value="true"/>
</bean>
freemarker.properties
#\u8BBE\u7F6E\u6807\u7B7E\u7C7B\u578B\uFF1Asquare_bracket:[] auto_detect:[]<>
tag_syntax=auto_detect
#\u6A21\u7248\u7F13\u5B58\u65F6\u95F4\uFF0C\u5355\u4F4D\uFF1A\u79D2
template_update_delay=0
default_encoding=UTF-8
output_encoding=UTF-8
locale=zh_CN
#\u8BBE\u7F6E\u6570\u5B57\u683C\u5F0F \uFF0C\u9632\u6B62\u51FA\u73B0 000.00
number_format=\#
#\u53D8\u91CF\u4E3A\u7A7A\u65F6\uFF0C\u4E0D\u4F1A\u62A5\u9519
classic_compatible=true
#\u8FD9\u4E2A\u8868\u793A\u6BCF\u4E2Afreemarker\u7684\u89C6\u56FE\u9875\u9762\u90FD\u4F1A\u81EA\u52A8\u5F15\u5165\u8FD9\u4E2Aftl\u6587\u4EF6\u3002\u91CC\u9762\u5B9A\u8BAE\u7684\u5C31\u662F\u4E00\u4E9B\u5B8F\uFF0C\u5982text\u6587\u672C\u6846\uFF0C\u5404\u79CDform\u5143\u7D20
#auto_import="/WEB-INF/templates/index.ftl" as do
@Controller
public class FreemarkerController {
@Resource
private UserService userService;
@RequestMapping("/index")
public String sayHello(ModelMap map){
map.put("name", "guest");
return "/index.ftl";
}
}
index.ftl
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>FreeMarker Web</title>
</head>
<body>
<h1>Hello ${name} !</h1>
</body>
</html>
输出标签
字符串输出:
${"Hello ${name} !"} / ${"Hello " + name + " !"}
<#assign cname=r"特殊字符完成输出(http:\www.baidu.com)">
${cname}
字符串截取 :
通过下标直接获取下标对应的字母: ${name[2]}
起点下标..结尾下标截取字符串:${name[0..5]}
算数运算:
<#-- 支持"+"、"-"、"*"、"/"、"%"运算符 -->
<#assign number1 = 10>
<#assign number2 = 5>
"+" : ${number1 + number2}
"-" : ${number1 - number2}
"*" : ${number1 * number2}
"/" : ${number1 / number2}
"%" : ${number1 % number2}
比较运算符:
<#if number1 + number2 gte 12 || number1 - number2 lt 6>
"*" : ${number1 * number2}
<#else>
"/" : ${number1 / number2}
</#if>
内建函数:
<#assign data = "abcd1234">
第一个字母大写:${data?cap_first}
所有字母小写:${data?lower_case}
所有字母大写:${data?upper_case}
<#assign floatData = 12.34>
数值取整数:${floatData?int}
获取集合的长度:${users?size}
时间格式化:${dateTime?string("yyyy-MM-dd")}
空判断和对象集合:
<#if users??>
<#list users as user >
${user.id} - ${user.name}
</#list>
<#else>
${user!"变量为空则给一个默认值"}
</#if>
Map集合:
<#assign mapData={"name":"程序员", "salary":15000}>
直接通过Key获取 Value值:${mapData["name"]}
通过Key遍历Map:
<#list mapData?keys as key>
Key: ${key} - Value: ${mapData[key]}
</#list>
通过Value遍历Map:
<#list mapData?values as value>
Value: ${value}
</#list>
List集合:
<#assign listData=["ITDragon", "blog", "is", "cool"]>
<#list listData as value>${value} </#list>
include指令:
引入其他文件:<#include "otherFreeMarker.ftl" />
macro宏指令:
<#macro mo>
定义无参数的宏macro--${name}
</#macro>
使用宏macro: <@mo />
<#macro moArgs a b c>
定义带参数的宏macro-- ${a+b+c}
</#macro>
使用带参数的宏macro: <@moArgs a=1 b=2 c=3 />
命名空间:
<#import "otherFreeMarker.ftl" as otherFtl>
${otherFtl.otherName}
<@otherFtl.addMethod a=10 b=20 />
<#assign otherName="修改otherFreeMarker.ftl中的otherName变量值"/>
${otherFtl.otherName}
<#assign otherName="修改otherFreeMarker.ftl中的otherName变量值" in otherFtl />
${otherFtl.otherName}
来源:CSDN
作者:liang2009go
链接:https://blog.csdn.net/liang2009go/article/details/104430963