51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1089|回复: 0
打印 上一主题 下一主题

Thymeleaf与SpringBoot集成

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2019-1-28 13:46:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
一:Thymeleaf与SpringBoot的集成

1.我们需要在Pom文件中加入如下依赖:

  1. <dependency>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>
复制代码

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

  1. #thymeleaf模板配置
  2. spring.thymeleaf.prefix=classpath:/templates/
  3. spring.thymeleaf.suffix=.html
  4. spring.thymeleaf.mode=LEGACYHTML5
  5. spring.thymeleaf.encoding=UTF-8
  6. spring.thymeleaf.content-type=text/html
  7. #热部署文件,页面不产生缓存,及时更新
  8. spring.thymeleaf.cache=false
  9. spring.resources.chain.strategy.content.enabled=true
  10. spring.resources.chain.strategy.content.paths=/**
复制代码

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

  1. <!DOCTYPE html>
  2. <html lang = "en" xmlns:th = "http://www.thymeleaf.org">
  3. <head>
  4.     <meta charset = "UTF-8">
  5.     <title>Title</title>
  6. </head>
  7. <body>
  8.     <h1 th:text = "${host}">Hello World</h1>
  9. </body>
  10. </html>
复制代码

4.写一个Controller方法测试:

  1. package com.flying.eurekaclient;

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

  5. @Controller
  6. @RequestMapping("thymeleaf")
  7. public class TestThymeleafController {
  8.     @RequestMapping("test_thymeleaf1")
  9.     public String test_thymeleaf1(Model model) {
  10.         model.addAttribute("host", "http://blog.didispace.com");
  11.         return "test_thymeleaf1";
  12.     }
  13. }
复制代码

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

  1. 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.
  2.         at org.thymeleaf.templateparser.html.AbstractHtmlTemplateParser.parseTemplate(AbstractHtmlTemplateParser.java:90) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
  3.         at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:278) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
  4.         at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
  5.         at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
  6.         at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE]
  7.         at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE]
  8.         at org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:190) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE]
  9.         at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1286) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
  10.         at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
  11.         at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
  12.         at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
  13.         at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
  14.         at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
  15.         at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  16.         at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
  17.         at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  18.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  19.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  20.         at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.31.jar:8.5.31]
  21.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  22.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  23.         at org.springframework.web.servlet.resource.ResourceUrlEncodingFilter.doFilter(ResourceUrlEncodingFilter.java:59) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
复制代码

原因:我们使用了如下配置

  1. spring.thymeleaf.mode=LEGACYHTML5
复制代码

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

但是使用这个配置我们需要引入nekoHTML依赖包,上面的异常提示的非常清楚,并且版本不能低于1.9.15,这个应该是与自己使用的SpringBoot版本有关(我的SpringBoot版本是1.5.14.RELEASE),按照错误提示我们在pom文件中加入如下依赖:

  1. <dependency>
  2.     <groupId>net.sourceforge.nekohtml</groupId>
  3.     <artifactId>nekohtml</artifactId>
  4.     <version>1.9.22</version>
  5. </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文件:

  1. <!DOCTYPE html>
  2. <html lang = "en" xmlns:th = "http://www.thymeleaf.org">
  3. <head>
  4.     <meta charset = "UTF-8">
  5.     <title>Title</title>
  6. </head>
  7. <body>
  8.     <h1 th:text = "${host}">Hello World</h1>
  9. </body>
  10. </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三种,且可以使用~{}表达式。


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-4-26 17:19 , Processed in 0.060271 second(s), 24 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表