Thymeleaf系列一 Spring boot 集成Thymeleaf

x33g5p2x  于2021-12-24 转载在 其他  
字(3.8k)|赞(0)|评价(0)|浏览(447)

1. 概念

1.1 静态资源

web的静态资源有 js、css、图片等
Spring Boot默认提供静态资源目录位置:

  • classpath:/META-INF/resources/
  • classpath:/resources/
  • classpath:/static/
  • classpath:/public/

定义在org.springframework.boot.autoconfigure.web.ResourceProperties类中:

  1. @ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
  2. public class ResourceProperties implements ResourceLoaderAware {
  3. private static final String[] SERVLET_RESOURCE_LOCATIONS = { "/" };
  4. private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
  5. "classpath:/META-INF/resources/", "classpath:/resources/",
  6. "classpath:/static/", "classpath:/public/" };
  7. private static final String[] RESOURCE_LOCATIONS;
  8. static {
  9. RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length
  10. + SERVLET_RESOURCE_LOCATIONS.length];
  11. System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0,
  12. SERVLET_RESOURCE_LOCATIONS.length);
  13. System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS,
  14. SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length);
  15. }
  16. /** * Locations of static resources. Defaults to classpath:[/META-INF/resources/, * /resources/, /static/, /public/] plus context:/ (the root of the servlet context). */
  17. private String[] staticLocations = RESOURCE_LOCATIONS;

1.2 模板引擎

Thymeleaf是Spring Boot提供的默认配置的模板引擎之一。模板默认的模板配置路径为:src/main/resources/templates。

这个路径配置在org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties 类中:

  1. @ConfigurationProperties(prefix = "spring.thymeleaf")
  2. public class ThymeleafProperties {
  3. private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");
  4. private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");
  5. public static final String DEFAULT_PREFIX = "classpath:/templates/";
  6. public static final String DEFAULT_SUFFIX = ".html";
  7. /** * Prefix that gets prepended to view names when building a URL. */
  8. private String prefix = DEFAULT_PREFIX;

2. 第一个demo

  • POM.XML
    配置依赖包
  1. <!-- 加入thymeleaf -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  5. </dependency>
  • 配置图片
    测试图片的位置:src/main/resources/static/images/leaf.jpg
  • 首页模板页面
    位置:src/main/resources/templates/simple/index.html
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
  3. <head lang="en">
  4. <meta charset="UTF-8" />
  5. <title></title>
  6. </head>
  7. <body>
  8. <!-- 通过thymeleaf引入图片路径 -->
  9. <img alt="test" th:src="@{images/leaf.jpg}" />
  10. <h1 th:text="${host}">Hello World</h1>
  11. </body>
  • Java的Control类
    输入/simple路径后台,转到index.html
  1. @RequestMapping("/simple")
  2. public String simpleIndex(ModelMap map) {
  3. // 加入一个属性,用来在模板中读取
  4. map.addAttribute("host", "http://blog.csdn.net/hry2015/article/");
  5. // return模板文件的名称,对应src/main/resources/templates/index.html
  6. return "simple/index";
  7. }
  • application.properties 关于thymeleaf配置
  1. # Enable template caching. 开发时,设置此值为false
  2. spring.thymeleaf.cache=false
  3. # Check that the templates location exists.
  4. spring.thymeleaf.check-template-location=true
  5. # Content-Type value.
  6. spring.thymeleaf.content-type=text/html
  7. # Enable MVC Thymeleaf view resolution.
  8. spring.thymeleaf.enabled=true
  9. # Template encoding.
  10. spring.thymeleaf.encoding=UTF-8
  11. # Comma-separated list of view names that should be excluded from resolution.
  12. spring.thymeleaf.excluded-view-names=
  13. # Template mode to be applied to templates. See also StandardTemplateModeHandlers.
  14. spring.thymeleaf.mode=HTML5
  15. # Prefix that gets prepended to view names when building a URL. 添加前缀到视图URL的前缀
  16. spring.thymeleaf.prefix=classpath:/templates/
  17. # Suffix that gets appended to view names when building a URL. 添加后缀到视图URL的后缀
  18. spring.thymeleaf.suffix=.html
  19. # Order of the template resolver in the chain.
  20. # spring.thymeleaf.template-resolver-order=
  21. # Comma-separated list of view names that can be resolved.
  22. # spring.thymeleaf.view-names=

3. 代码

github

相关文章