Thymeleaf系列一 Spring boot 集成Thymeleaf

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

1. 概念

1.1 静态资源

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

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

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

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties implements ResourceLoaderAware {

    private static final String[] SERVLET_RESOURCE_LOCATIONS = { "/" };

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
        "classpath:/META-INF/resources/", "classpath:/resources/",
        "classpath:/static/", "classpath:/public/" };

    private static final String[] RESOURCE_LOCATIONS;

    static {
        RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length
            + SERVLET_RESOURCE_LOCATIONS.length];
        System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0,
            SERVLET_RESOURCE_LOCATIONS.length);
        System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS,
            SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length);
    }

    /** * Locations of static resources. Defaults to classpath:[/META-INF/resources/, * /resources/, /static/, /public/] plus context:/ (the root of the servlet context). */
private String[] staticLocations = RESOURCE_LOCATIONS;

1.2 模板引擎

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

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

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

    private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");

    private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");

    public static final String DEFAULT_PREFIX = "classpath:/templates/";

    public static final String DEFAULT_SUFFIX = ".html";
    /** * Prefix that gets prepended to view names when building a URL. */
    private String prefix = DEFAULT_PREFIX;

2. 第一个demo

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

3. 代码

github

相关文章