大纲

(1)在pom.xml中引入freemarker;

(2)如何关闭freemarker缓存

(3)编写模板文件.ftl

(4)编写访问文件的controller

在pom.xml中引入freemarker

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

如何关闭freemarker缓存

########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
#spring.freemarker.suffix=.ftl
#spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved

编写模板文件.ftl

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello.v.2</h1>
<p>${hello}</p>
</body>
</html>

编写访问文件的controller

@RequestMapping("/helloFtl")
public String helloFtl(Map<String,Object> map){
map.put("hello","from TemplateController.helloFtl");
return "/helloFtl";
}

代码如下:

Controller类

package com.kfit;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; /**
* 注意:
* 1.在Thymeleaf 模板文件中,标签是需要闭合的,3.0之前是需要闭合的
* 2. thymeleaf 3.0+ 是可以不强制要求闭合的。
*
* 3. 支持同时使用多个模板引擎,比如:thymeleaf和freemarker 可以并存。
* */
@Controller
@RequestMapping("/templates")
public class TemplatesController { /**
* 映射地址是:/templates/hello
* @return
*/
@RequestMapping("/hello")
public String hello(Map<String,Object> map){
//返回的是ModelAndView对象;
// ModelAndView mv = new ModelAndView("hello");
// return mv;
map.put("name","Andy");
return "hello";
} @RequestMapping("/helloFtl")
public String helloFtl(Map<String,Object> map){
map.put("name","Andy");
return "helloFtl";
} }

模板

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
hello ${name} </body>
</html>

配置

########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
spring.freemarker.allow-request-override=false
spring.freemarker.cache=false
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
spring.freemarker.suffix=.ftl
#spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved

最新文章

  1. 项目在vs中打开后识别不出来ashx页面的解决方法
  2. throw和throws的区别
  3. Epic - Spiral Matrix
  4. POJ 2342 Anniversary party (树dp)
  5. Microsoft.Practices.EnterpriseLibrary.Logging的使用
  6. 鸟哥笔记:syslogd:记录日志文件的服务
  7. BZOJ_1616_[Usaco2008_Mar]_Cow_Travelling_游荡的奶牛_(DP)
  8. require include 一个隐藏的用法:作用域。
  9. HrbustOJ 1564 螺旋矩阵
  10. [Android] Android Studio 使用config.gradle统一管理项目的依赖库
  11. 洛谷 P1053 音乐会的等待 解题报告
  12. noip第17课作业
  13. Spring Boot中使用@Scheduled创建定时任务
  14. weblogic92一次成功修改密码的记录
  15. BZOJ.3489.A simple rmq problem(主席树 Heap)
  16. 自动化运维之 ~cobbler~
  17. 【JavaScript】事件
  18. SQL 按照多个字段为标准的重复数据处理
  19. [ 原创 ] Linux下查找指定类型文件以及删除
  20. CF666E Forensic Examination(后缀自动机+动态线段树)

热门文章

  1. Java重要类之LinkedList
  2. 使用Jsoup爬取网站图片
  3. Webpack loaderUtils.parseQuery()
  4. H5的video标签在网页上播放MP4视频时只有声音没有画面
  5. Linux进程管理工具之ps
  6. PAT Basic 1020 月饼 (25 分)
  7. 第三方库-时间函数dateutil
  8. iOS中为控件设置颜色渐变和透明度渐变
  9. 【洛谷P4173】残缺的字符串
  10. beanFactory &amp; FactoryBean区别