1、简介
FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页、电子邮件、配置文件、源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。(--摘自百度百科)
2、配置
(1)本人使用的idea创建的maven应用。所以首先引入freemarker包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>1.5.8.RELEASE</version>
</dependency>
(2)配置application.properties/application.yml,还有其他的设置,具体请查相关文档
classpath:是相对路径
file:是绝对路径
freemarker:
template-loader-path:classpath: ./templates/
cache: false
charset: UTF-8
check-template-location: true
content-type: text/html
expose-request-attributes: false
expose-session-attributes: false
suffix: .ftl
allow-request-override: false
expose-spring-macro-helpers: false
request-context-attribute: request
(3)文件架构
templates文件中主要是ftl文件,也就是前端页面。static中主要是一些静态文件,css,js,图片啊一些文件。
(4)调用静态文件,并不需要写static文件目录
<link rel="stylesheet" href="../css/hello.css">
(5)接下来就可以直接在controller的调用了
@RequestMapping("/helloBoy")
public String hello(){
return "hello";
}
(6)测试