Apache FreeMarker™是一个模板引擎:一个Java库,用于根据模板和更改数据生成文本输出(HTML网页,电子邮件,配置文件,源代码等)。模板是用FreeMarker模板语言(FTL)编写的,这是一种简单的专用语言(不像PHP这样的完整编程语言)。通常,使用通用编程语言(如Java)来准备数据(发布数据库查询,进行业务计算)。然后,Apache FreeMarker使用模板显示准备好的数据。在模板中,您将专注于如何呈现数据,而在模板之外,您将专注于要呈现的数据。------官网
# ------ 普通和对象类型 -------
<h1>您好:${user.name}h1>
// 对象类型 数据封装
User user = new User();
user.setId(1);
user.setName("⼩小红");
datas.put("user",user);
# ------ 数字类型 ------
# assign ⽤用以声明当前模板临时变量量
<#assign answer=42>
${answer?string} <#-- the same as ${answer} -->
${answer?string.number}
${answer?string.currency}
${answer?string.percent}
# ------ 日期类型 ------
${date?date}
${date?time}
${date?datetime}
# ⾃自定义日期格式
${date?string("yyyy年年MM⽉月dd⽇日")}
# ------ 布尔值处理理 ------
# 获取实际值 true | false
${b?c}
# 根据boolean值获取值
${b?string('yes', 'no')}
<#list 集合名 as 别名>
${别名}
#list>
// 集合类型
ArrayList<String> list = new ArrayList<String>();
list.add("apple");
list.add("banana");
list.add("orange");
datas.put("fruit",list);
<#list map?keys as city>
${city} ---> ${map[city]} <br>
#list>
// map集合
Map<String,Object> map = new HashMap<String,Object>();
map.put("beijing","北北京");
map.put("天津","天津");
datas.put("map",map);
<#if 条件>
// true内容
<#else>
// false内容
#if>
${ key名!"默认值"}
<#if test??>
值存在
<#else>
不存在
#if>
<#include“TemplateFiles/example.ftl”>
# 如
<#include "test.html">
<dependency>
<groupId>org.freemarkergroupId>
<artifactId>freemarkerartifactId>
<version>2.3.23version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.3.11.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>4.3.11.RELEASEversion>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
<scope>providedscope>
dependency>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.controller">context:component-scan>
<mvc:annotation-driven/>
<bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="defaultEncoding" value="utf-8">property>
<property name="templateLoaderPath" value="/WEB-INF/template/">property>
bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="prefix" value="/">property>
<property name="suffix" value=".ftl">property>
<property name="contentType" value="text/html;charset=utf-8">property>
bean>
beans>
小坑:IDEA自动导入mvc的xmlns时,会导错,证据如下
别看了,你复制的是对的
<html>
<head>
<meta charset="UTF-8">
head>
<body>
hello:${words}
<hr>
age:${age}
<hr>
exists:${exists ?string("存在","不存在") }
<hr>
salary:${salary}
body>
html>
@Controller
public class FtlController {
@RequestMapping("hello")
public ModelAndView testHello(ModelAndView modelAndView){
//设置要是用的模板文件(不加后缀)
modelAndView.setViewName("index");
modelAndView.addObject("words","你好呀,傻子");
modelAndView.addObject("age",18);
modelAndView.addObject("exists",true);
modelAndView.addObject("salary",15000.0);
return modelAndView;
}
}
简直不要太简单
spring.freemarker.prefix = /
server.port=8989
<html>
<head>
<meta charset="UTF-8">
head>
<body>
hello:${words}
<hr>
age:${age}
<hr>
exists:${exists ?string("存在","不存在") }
<hr>
salary:${salary}
body>
html>
跟上面mvc的一样
@Controller
public class FtlController {
@RequestMapping("hello")
public ModelAndView testHello(ModelAndView modelAndView) {
modelAndView.setViewName("index");
modelAndView.addObject("words", "你好呀,傻子");
modelAndView.addObject("age", 18);
modelAndView.addObject("exists", true);
modelAndView.addObject("salary", 15000.0);
return modelAndView;
}
success -_-