整体步骤:

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

(2)            如何关闭thymeleaf缓存

(3)            编写模板文件.html

Spring Boot默认就是使用thymeleaf模板引擎的,所以只需要在pom.xml加入依赖即可:

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

Thymeleaf缓存在开发过程中,肯定是不行的,那么就要在开发的时候把缓存关闭,只需要在application.properties进行配置即可:

########################################################

###THYMELEAF (ThymeleafAutoConfiguration)

########################################################

#spring.thymeleaf.prefix=classpath:/templates/

#spring.thymeleaf.suffix=.html

#spring.thymeleaf.mode=HTML5

#spring.thymeleaf.encoding=UTF-8

# ;charset=<encoding> is added

#spring.thymeleaf.content-type=text/html

# set to false for hot refresh

spring.thymeleaf.cache=false

编写模板文件src/main/resouces/templates/helloHtml.html

<!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 th:inline="text">Hello.v.2</h1>

<p th:text="${hello}"></p>

</body>

</html>

编写访问路径(com.kfit.test.web.TemplateController):

package com.kfit.test.web;

import java.util.Map;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

/**

* 模板测试.

* @author Administrator

*

*/

@Controller

publicclass TemplateController {

/**

* 返回html模板.

*/

@RequestMapping("/helloHtml")

public String helloHtml(Map<String,Object> map){

map.put("hello","from TemplateController.helloHtml");

return"/helloHtml";

}

}

启动应用,输入地址:http://127.0.0.1:8080/helloHtml 会输出:

Hello.v.2

from TemplateController.helloHtml

18.2 使用freemarker

使用freemarker也很简单,

在pom.xml加入freemarker的依赖:

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-freemarker</artifactId>

</dependency>

剩下的编码部分都是一样的,说下application.properties文件:

########################################################

###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-separatedlist

#spring.freemarker.view-names= #whitelistofviewnamesthatcanberesolved

com.kfit.test.web.TemplateController:

/**

* 返回html模板.

*/

@RequestMapping("/helloFtl")

public String helloFtl(Map<String,Object> map){

map.put("hello","from TemplateController.helloFtl");

return"/helloFtl";

}

src/main/resouces/templates/helloFtl.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>

访问地址:http://127.0.0.1:8080/helloFtl

Hello.v.2

from TemplateController.helloFtl

thymeleaf和freemarker是可以共存的。

Spring Boot 系列博客】

58. Spring Boot国际化(i18n)【从零开始学Spring Boot】

57. Spring 自定义properties升级篇【从零开始学Spring Boot】

56. spring boot中使用@Async实现异步调用【从零开始学Spring Boot】

55. spring boot 服务配置和部署【从零开始学Spring Boot】

54. spring boot日志升级篇—logback【从零开始学Spring Boot】

52. spring boot日志升级篇—log4j多环境不同日志级别的控制【从零开始学Spring Boot】

51. spring boot属性文件之多环境配置【从零开始学Spring Boot】

50. Spring Boot日志升级篇—log4j【从零开始学Spring Boot】

49. spring boot日志升级篇—理论【从零开始学Spring Boot】

48. spring boot单元测试restfull API【从零开始学Spring Boot】

47. Spring Boot发送邮件【从零开始学Spring Boot】

46. Spring Boot中使用AOP统一处理Web请求日志

45. Spring Boot MyBatis连接Mysql数据库【从零开始学Spring Boot】

44. Spring Boot日志记录SLF4J【从零开始学Spring Boot】

43. Spring Boot动态数据源(多数据源自动切换)【从零开始学Spring Boot】

42. Spring Boot多数据源【从零开始学Spring Boot】

41. Spring Boot 使用Java代码创建Bean并注册到Spring中【从零开始学Spring Boot】

40. springboot + devtools(热部署)【从零开始学Spring Boot】

39.4 Spring Boot Shiro权限管理【从零开始学Spring Boot】

39.3 Spring Boot Shiro权限管理【从零开始学Spring Boot】

39.2. Spring Boot Shiro权限管理【从零开始学Spring Boot】

39.1 Spring Boot Shiro权限管理【从零开始学Spring Boot】

38 Spring Boot分布式Session状态保存Redis【从零开始学Spring Boot】

37 Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】

36 Spring Boot Cache理论篇【从零开始学Spring Boot】

35 Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

34Spring Boot的启动器Starter详解【从零开始学Spring Boot】

33 Spring Boot 监控和管理生产环境【从零开始学Spring Boot】

32 Spring Boot使用@SpringBootApplication注解【从零开始学Spring Boot】

31 Spring Boot导入XML配置【从零开始学Spring Boot】

更多查看博客: http://412887952-qq-com.iteye.com/

最新文章

  1. RSA算法学习
  2. SQL Server快捷键
  3. php 上传大文件配置upload_max_filesize和post_max_size选项
  4. iOS开发工具篇-AppStore统计工具 (转载)
  5. iOS学习之手势
  6. 在UI线程之外显示Toast
  7. eclips中增加对jar包的引用
  8. 细说Debug和Release区别
  9. 【BZOJ】【1024】【SCOI2009】生日快乐
  10. 安装sybase12.0,运行时报错异常。
  11. 22.allegro中PCB打印设置[原创]
  12. HTML+CSS基础学习笔记(4)
  13. Servlet 浅谈(三)
  14. bzoj 1046 : [HAOI2007]上升序列 dp
  15. 【Linux】 linux的进程系统一点补充
  16. 【Android】ContentValues的用法
  17. python实现微信接口——itchat模块
  18. windows下搭建docker
  19. Kaggle(2):验证和过分拟合
  20. Web API中常用Filter的执行顺序举例讲解

热门文章

  1. mac Homebrew Updating慢,替换及重置Homebrew默认源
  2. STL_算法_逆转(reverse,reverse_copy)
  3. 查看及改动Oracle编码格式方法
  4. Android Calendar的运用
  5. C++&lt;iomanip&gt;控制符
  6. Coursera Algorithms week2 基础排序 练习测验: Dutch national flag 荷兰国旗问题算法
  7. 第3课 把文件存入Git文档库
  8. c++调用DOS命令,不显示黑屏
  9. 使用node成功安装完某插件typescript后,在使用时提示:tsc(或xxx)不是内部或外部命令,也不是可运行的程序或批处理文件
  10. jboss-as-7.1.1.Final配置Jndi数据源(以mysql为例)