这篇文章将为大家详细讲解有关thymeleaf模板如何春天在引导中使用,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
<强>前言强>
thymeleaf是一个跟速度,FreeMarker类似的模板引擎,它可以完全替代JSP。相较与其他的模板引擎,它有如下三个极吸引人的特点:
,,,,,1.Thymeleaf在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持html原型,然后在html标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释html时会忽略未定义的标签属性,所以Thymeleaf的模板可以静态地运行;当有数据返回到页面时,Thymeleaf标签会动态地替换掉静态内容,使页面动态显示。
,,,,,2.Thymeleaf开箱即用的特性。它提供标准和春天标准两种方言,可以直接套用模板实现JSTL, OGNL表达式效果,避免每天套模板,该JSTL,改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
,,,,,3.Thymeleaf提供春标准方言和一个与SpringMVC完美集成的可选模块,可以快速的实现表单绑定,属性编辑器,国际化等功能。
下面这篇文章将给
<强>整体步骤:强>
(1),在pom.xml中引入thymeleaf;
(2),,如何关闭thymeleaf缓存
(3),,编写模板文件。html
弹簧引导默认就是使用thymeleaf模板引擎的,所以只需要在砰的一声。xml加入依赖即可:
& lt; dependency>, & lt;才能groupId> org.springframework.boot</groupId>, , & lt;才能artifactId> spring-boot-starter-thymeleaf</artifactId>, & lt;/dependency>
Thymeleaf缓存在开发过程中,肯定是不行的,那么就要在开发的时候把缓存关闭,只需要在应用程序中。特性进行配置即可:
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #, # # # THYMELEAF (ThymeleafAutoConfiguration), # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #, # spring.thymeleaf.prefix=类路径:/模板/, # spring.thymeleaf.suffix=.html # spring.thymeleaf.mode=HTML5 # spring.thymeleaf.encoding=UTF-8 #,;charset=& lt; encoding>, is added # spring.thymeleaf.content-type=text/html #,set 用false for 热;refresh , spring.thymeleaf.cache=false
编写模板文件src/main/资源/模板/helloHtml。html
& lt; ! DOCTYPE html>, & lt; html  xmlns=癶ttp://www.w3.org/1999/xhtml", xmlns: th=? http://www.thymeleaf.org",, ,xmlns:秒=癶ttp://www.thymeleaf.org/thymeleaf-extras-springsecurity3"祝辞, & lt; head>大敌; ,& lt; title> Hello 世界! & lt;/title>, & lt;/head>大敌; & lt; body>大敌; ,& lt; h2  th:内联=皌ext"祝辞Hello.v.2, ,& lt; p  th:文本=?{你好}“祝辞& lt;/p>, & lt;/body>大敌; & lt;/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<字符串,Object>,地图){, , ,map.put (“hello"“得到TemplateController.helloHtml"),, ,return"/helloHtml"; }大敌; }
启动应用,输入地址:http://127.0.0.1:8080 helloHtml会输出:
Hello.v。从TemplateController.helloHtml 2
<强>使用freemarker 强>
使用freemarker也很简单,
在pom。xml加入freemarker的依赖:
& lt; dependency>, ,& lt; groupId> org.springframework.boot, ,& lt; artifactId> spring-boot-starter-freemarker, & lt;/dependency>
剩下的编码部分都是一样的,说下应用程序。属性文件:
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #, # # # 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=类路径:/模板/# comma-separatedlist # spring.freemarker.view-names=, # whitelistofviewnamesthatcanberesolvedthymeleaf模板如何春天在引导中使用