Thymeleaf与SpringBoot集成

序:css

Thymeleaf官方参考文档:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#a-multi-language-welcomehtml

关于thymeleaf2.x与thymeleaf3.x的区别(官方文档):https://www.thymeleaf.org/doc/articles/thymeleaf3migration.htmljava

正文:web

一:Thymeleaf与SpringBoot的集成

1.咱们须要在Pom文件中加入以下依赖:spring

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

2.application.properties配置文件中加入以下配置:apache

#thymeleaf模板配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#热部署文件,页面不产生缓存,及时更新
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

3.在templates目录下写一个测试文件:缓存

<!DOCTYPE html>
<html lang = "en" xmlns:th = "http://www.thymeleaf.org">
<head>
    <meta charset = "UTF-8">
    <title>Title</title>
</head>
<body>
    <h1 th:text = "${host}">Hello World</h1>
</body>
</html>

4.写一个Controller方法测试:tomcat

package com.flying.eurekaclient;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("thymeleaf")
public class TestThymeleafController {
    @RequestMapping("test_thymeleaf1")
    public String test_thymeleaf1(Model model) {
        model.addAttribute("host", "http://blog.didispace.com");
        return "test_thymeleaf1";
    }
}

5.启动SpringBoot项目,发现出现以下异常:websocket

org.thymeleaf.exceptions.ConfigurationException: Cannot perform conversion to XML from legacy HTML: The nekoHTML library is not in classpath. nekoHTML 1.9.15 or newer is required for processing templates in "LEGACYHTML5" mode [http://nekohtml.sourceforge.net]. Maven spec: "net.sourceforge.nekohtml::nekohtml::1.9.15". IMPORTANT: DO NOT use versions of nekoHTML older than 1.9.15.
	at org.thymeleaf.templateparser.html.AbstractHtmlTemplateParser.parseTemplate(AbstractHtmlTemplateParser.java:90) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:278) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:190) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1286) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.31.jar:8.5.31]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
	at org.springframework.web.servlet.resource.ResourceUrlEncodingFilter.doFilter(ResourceUrlEncodingFilter.java:59) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]

缘由:咱们使用了以下配置mvc

spring.thymeleaf.mode=LEGACYHTML5

,这个配置表示咱们写的thymeleaf模版不须要严格执行HTML5标准的语法校验,

可是使用这个配置咱们须要引入nekoHTML依赖包,上面的异常提示的很是清楚,而且版本不能低于1.9.15,这个应该是与本身使用的SpringBoot版本有关(个人SpringBoot版本是1.5.14.RELEASE),按照错误提示咱们在pom文件中加入以下依赖:

<dependency>
    <groupId>net.sourceforge.nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
    <version>1.9.22</version>
</dependency>

重启项目,访问http://localhost:8762/thymeleaf/test_thymeleaf1,正确显示以下:

Tips:

这里咱们写的html文件做为thymeleaf模版文件进行解析,那要是咱们要访问写的静态HTML文件怎么办呢,解决方案以下:

1.咱们能够将静态HTML文件也放入templates目录下,可是这样的话咱们对每个HTML文件的访问都须要写一个controller方法去映射该静态文件的访问路径,这样无疑是多余的,此方法不可取。

2.SpringBoot给咱们提供了专门用于访问静态资源文件的目录,即static目录,咱们只须要将静态文件(静态html,css,js,图片文件等)放入到static目录下便可,可是咱们访问静态文件时不须要加上路径/static,SpringBoot会自动到该目录去找。

以下图所示,咱们在static目录下新建一个目录pages,而后在该目录下新建一个静态html文件:

<!DOCTYPE html>
<html lang = "en" xmlns:th = "http://www.thymeleaf.org">
<head>
    <meta charset = "UTF-8">
    <title>Title</title>
</head>
<body>
    <h1 th:text = "${host}">Hello World</h1>
</body>
</html>

访问http://localhost:8762/pages/test_thymeleaf2.html,页面正确显示:

二:在SpringBoot中使用Thymeleaf的i18n国际化功能

1.在SpringBoot的配置文件中加入以下配置(对应本身的国际化信息文件目录):

对应的国际化信息文件以下图所示:

spring.messages.basename的默认值是message,且国际化信息文件在resources根目录下。

2.在thymeleaf模版文件中使用国际化,如图所示(使用#表达式):

#表达式还有其余用法,详情参见博文开头提供的官方文档连接。

3.显示效果以下:

国际化信息文件内容以下:

Tips:

国际化信息文件必须提供默认信息文件,不然国际化功能没法正常使用,即不带相似国家后缀"zh_CN"等。

以下图所示:

三:使用Thymeleaf的片断(th:include , th:replace, th:insert)功能注意事项

1.Thymeleaf2.x版本里只支持th:include, th:replace,不支持th:insert,且不能使用~{}表达式,例如不能像以下图所示这样写:

,只能这样写:

2..Thymeleaf3.x版本里支持th:include, th:replace,th:insert三种,且可使用~{}表达式。

更多关于thymeleaf2.x与thymeleaf3.x的区别详见官方文档:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html

相关文章
相关标签/搜索