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