整体步骤:

(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/

(18)使用模板(thymeleaf-freemarker)【从零开始学Spring Boot】的更多相关文章

  1. Spring Boot使用模板freemarker【从零开始学Spring Boot(转)

    视频&交流平台: à SpringBoot网易云课堂视频 http://study.163.com/course/introduction.htm?courseId=1004329008 à  ...

  2. 77. Spring Boot Use Thymeleaf 3【从零开始学Spring Boot】

    [原创文章,转载请注明出处] Spring Boot默认选择的Thymeleaf是2.0版本的,那么如果我们就想要使用3.0版本或者说指定版本呢,那么怎么操作呢?在这里要说明下 3.0的配置在spri ...

  3. 68. 使用thymeleaf报异常:Not Found, status=404【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 我们按照正常的流程编码好了 controller访问访问方法/hello,对应的是/templates/hello.html文件,但是在页面中还是 ...

  4. 59. Spring Boot Validator校验【从零开始学Spring Boot】

    大纲: (1) 入门例子: (2) 国际化: (3) 在代码中添加错误信息: (1) 入门例子: Validator主要是校验用户提交的数据的合理性的,比如是否为空了,密码长度是否大于6位,是否是纯数 ...

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

    国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素.换言之,应用程序的功能和代码设计考虑在不 ...

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

    (提供源代码) Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看如何在Spring Bo ...

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

    [本文章是否对你有用以及是否有好的建议,请留言] 本文章牵涉到的技术点比较多:Spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对 ...

  8. (29)Spring boot 文件上传(多文件上传)【从零开始学Spring Boot】

    文件上传主要分以下几个步骤: (1)新建maven java project: (2)在pom.xml加入相应依赖: (3)新建一个表单页面(这里使用thymleaf); (4)编写controlle ...

  9. (19)Spring Boot 添加JSP支持【从零开始学Spring Boot】

    [来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论: 您的认可是我最大的动力,感谢您的支持] 看完本文章您可能会有些疑问,可以查看之后的一篇博客: 81. Spring Boot集成JSP疑问[从 ...

随机推荐

  1. Java-CyclicBarrier的简单样例

    内容:一个主任务等待两个子任务,通过CyclicBarrier的await()实现.此Runnable任务在CyclicBarrier的数目达到后,全部其他线程被唤醒前被运行. public clas ...

  2. Android之使用MediaMetadataRetriever类获取视频第一帧

    一.首先,来介绍一下MediaMetadataRetriever类,此类位于android.media包下,这里,先附上可查看此类的API地址:MediaMetadataRetriever类.大家能够 ...

  3. 【字符串处理】AC自动机知识点&代码

    代码: #include<iostream> #include<vector> #include<cstdio> #include<queue> #in ...

  4. insufficient space

  5. Find Minimum in Rotated Sorted Array 典型二分查找

    https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Suppose a sorted array is rot ...

  6. PCB 全景图技术实现

    为了对3D模型理解更透,这里采用threejs(WebGL第三方库)实现,刚开始学习入门,为了能看明白基本上每行代码都注释. 如果仅仅是为了实现全景图,可以用photo-sphere-viewer.j ...

  7. Pet(dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=4707 题意:判断距离大于D的点有多少个. 思路: 邻接表建图,dfs每一个点,记录步数. #include &l ...

  8. 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall(二分图)

    P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...

  9. PHP CURL抓取网页 simple_html_dom类

    抓取网页数据后 数据录入到discuz中 <?php include('simple_html_dom.php'); function urlText(){ $url = 'http://www ...

  10. Oracle 当输入参数允许为空时

    场景: 有一个存储过程p_test 带有多个输入参数code.name.number p_test(code IN VARCHAR2,nameIN VARCHAR2,number IN VARCHAR ...