基本步骤
- 添加pom依赖
- 在application.yml中添加相关配置
- 创建freemarker模板
- 创建控制层
- 测试访问
添加pom依赖
<!-- springboot整合freemarker -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
在application.yml中添加相关配置
# 配置freemarker
spring:
freemarker:
# 设置模板后缀名
suffix: .ftl
# 设置文档类型
content-type: text/html
# 设置页面编码格式
charset: UTF-8
# 设置页面缓存
cache: false
# 设置ftl文件路径
template-loader-path:
- classpath:/templates
# 设置静态文件路径,js,css等
mvc:
static-path-pattern: /static/**
创建freemarker模板
目录:src/main/resources 创建templates文件夹,文件夹里新建freemarker.ftl文件
<!DOCTYPE>
<html>
<head>
<title>freemark</title>
</head>
<body>
<h1>Hello ${name} from resource freemark!</h1>
</body>
</html>
创建控制层
package com.ahut.action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
*
* @ClassName: FreemarkerAction
* @Description: freemarker控制层
* @author cheng
* @date 2018年1月22日 下午8:19:39
*/
@Controller
@RequestMapping(value = "/freemarker")
public class FreemarkerAction {
/**
* 日志管理
*/
private static Logger log = LoggerFactory.getLogger(FreemarkerAction.class);
/**
*
* @Title: toDemo
* @Description: 跳转freemarker页面
* @param mv
* @return
*/
@RequestMapping(value = "/toDemo")
public ModelAndView