Changing the Thymeleaf Template Directory in Spring Boot – 改变Spring Boot中的Thymeleaf模板目录

最后修改: 2019年 7月 28日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

Thymeleaf is a template engine that we can use for our Spring Boot applications. As with many things, Spring Boot provides a default location where it expects to find our templates.

Thymeleaf是一个模板引擎,我们可以将其用于Spring Boot应用程序。与许多事情一样,Spring Boot提供了一个默认位置,它希望在那里找到我们的模板

In this short tutorial, we’re going to look at how we can change the template location. After we do that, we’ll learn how to have multiple locations.

在这个简短的教程中,我们要看一下如何改变模板的位置。做完这些,我们将学习如何拥有多个位置。

2. Setup

2.设置

To use Thymeleaf, we need to add the appropriate Spring Boot starter to our pom.xml:

要使用Thymeleaf,我们需要将合适的Spring Boot启动器添加到我们的pom.xml

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

3. Changing the Default Location

3.改变默认位置

By default, Spring Boot looks for our templates in src/main/resources/templates. We can put our templates there and organize them in sub-directories and have no issues.

默认情况下,Spring Boot会在src/main/resources/templates中寻找我们的模板。我们可以将我们的模板放在那里,并在子目录中组织它们,这样就不会有问题。

Now, let’s imagine that we have a requirement that all our templates reside in a directory called templates-2.

现在,让我们想象一下,我们有一个要求,即所有的模板都驻扎在一个叫做templates-2的目录中。

Let’s create a template for saying hello and put it in src/main/resources/templates-2:

让我们创建一个打招呼的模板,把它放在src/main/resources/templates-2中。

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Enums in Thymeleaf</title>
</head>
<body>
    <h2>Hello from 'templates/templates-2'</h2>
</body>
</html>

We’ll also need a controller:

我们还需要一个控制器。

@GetMapping("/hello")
public String sayHello() {
    return "hello";
}

With that basic setup out of the way, let’s configure Spring Boot to use our templates-2 directory by overriding a property in application.properties:

有了这些基本设置,让我们通过覆盖application.properties中的一个属性来配置Spring Boot,以使用我们的templates-2目录。

spring.thymeleaf.prefix=classpath:/templates-2/

Now, when we call our HelloController, we’ll see our greeting from hello.html.

现在,当我们调用HelloController时,我们会看到hello.html中的问候语。

4. Using Multiple Locations

4.使用多个地点

Now that we’ve learned how to change the default location, let’s see how we can use multiple template locations.

现在我们已经学会了如何改变默认位置,让我们看看如何使用多个模板位置。

To do this, let’s create a ClassLoaderTemplateResolver bean:

要做到这一点,让我们创建一个ClassLoaderTemplateResolverbean。

@Bean
public ClassLoaderTemplateResolver secondaryTemplateResolver() {
    ClassLoaderTemplateResolver secondaryTemplateResolver = new ClassLoaderTemplateResolver();
    secondaryTemplateResolver.setPrefix("templates-2/");
    secondaryTemplateResolver.setSuffix(".html");
    secondaryTemplateResolver.setTemplateMode(TemplateMode.HTML);
    secondaryTemplateResolver.setCharacterEncoding("UTF-8");
    secondaryTemplateResolver.setOrder(1);
    secondaryTemplateResolver.setCheckExistence(true);
        
    return secondaryTemplateResolver;
}

In our custom bean, we set our prefix to the secondary template directory we’re using: templates-2. We also set the CheckExistance flag to true. This is the key to allowing the resolvers to operate in a chain.

在我们的自定义Bean中,我们将前缀设置为我们正在使用的二级模板目录。templates-2. 我们还将CheckExistance标志设置为true。这是允许解析器以连锁方式运行的关键。

With this configured, our application can use templates from the default main/resources/templates directory and main/resources/templates-2.

通过这样的配置,我们的应用程序可以使用默认的main/resources/templates目录和main/resources/templates-2中的模板。

5. Errors

5.错误

When we’re working with Thymeleaf, we might see this error:

当我们用Thymeleaf工作时,我们可能会看到这个错误。

Error resolving template [hello], template might not exist or might not be accessible
  by any of the configured Template Resolvers

We see this message when Thymeleaf cannot locate the template for some reason. Let’s look at some of the possible reasons for this and how to fix them.

当Thymeleaf由于某种原因无法找到模板时,我们会看到这个消息。让我们看看一些可能的原因,以及如何解决这些问题。

5.1. Typo in the Controller

5.1.控制器中的错别字

We can often see this error due to a simple typo. The first thing to check is that our file name minus the extension and the template we’re asking for in our controller match exactly. If we’re using subdirectories, we need to ensure that those are correct as well.

我们经常可以看到这个错误是由于一个简单的打字错误。首先要检查的是,我们的文件名减去扩展名和我们在控制器中要求的模板是否完全匹配。如果我们使用的是子目录,我们需要确保这些子目录也是正确的。

Additionally, the problem can be an issue with certain operating systems. Windows is not case sensitive, but other operating systems are. We should look into this if everything is working fine, say, on our local Windows machine, but not once we’ve deployed.

此外,这个问题可能是某些操作系统的问题。Windows对大小写不敏感,但其他操作系统是敏感的。如果一切工作正常,例如在我们本地的Windows机器上,但一旦我们部署后就不正常了,我们应该研究这个问题。

5.2. Including the File Extension in the Controller

5.2.在控制器中包括文件扩展名

Since our files typically have an extension, it can be natural to include them when we return our template path in the controller. Thymeleaf automatically appends the suffix, so we should avoid supplying it.

由于我们的文件通常有一个扩展名,当我们在控制器中返回模板路径时,可以很自然地包括它们。Thymeleaf会自动附加后缀,所以我们应该避免提供它

5.3. Not Using the Default Location

5.3.不使用默认位置

We’ll also see this error if we’ve placed our templates somewhere other than src/main/resources/templates. If we want to use a different location, we need to either set the spring.thymeleaf.prefix property or create our own ClassLoaderTemplateResolver bean to handle multiple locations.

如果我们将模板放在src/main/resources/templates以外的地方,我们也会看到这个错误。如果我们想使用不同的位置,我们需要设置spring.thymeleaf.prefix属性或创建我们自己的ClassLoaderTemplateResolverbean来处理多个位置。

6. Conclusion

6.结论

In this quick tutorial, we learned about Thymeleaf template locations. First, we saw how to change the default location by setting a property. Then we built on that by creating our own ClassLoaderTemplateResolver to use multiple locations.

在这个快速教程中,我们了解了Thymeleaf模板的位置。首先,我们看到了如何通过设置一个属性来改变默认的位置。然后,我们在此基础上创建了自己的ClassLoaderTemplateResolver来使用多个位置。

We wrapped up with a discussion of the error we’ll see when Thymeleaf can’t find our templates and how to solve it.

最后,我们讨论了当Thymeleaf找不到我们的模板时,我们会看到的错误以及如何解决这个问题。

As always, the example code can be found over on GitHub.

一如既往,可以在GitHub上找到示例代码。