默认的Spring Boot Thymeleaf配置位置

huangapple go评论140阅读模式
英文:

Default Spring Boot Thymeleaf configuration location

问题

我曾经使用JSP很长时间,但现在才尝试使用Thymeleaf。我发现对于我的应用程序,我需要在application.properties文件中进行一些调整,我已经弄清楚了Spring Boot Thymeleaf的设置方法。

但是我在哪里可以找到Spring Boot默认使用的Thymeleaf设置的权威完整列表呢?例如,我知道Spring Boot配置Thymeleaf在类路径上的/templates/目录中查找模板,我也知道如何更改它。但是默认的/templates/在哪里配置的呢?是否有一个DefaultXXX类或.properties文件我可以打开来查看这些设置是如何设置的,如果我不进行任何更改,会使用哪些值?

英文:

I've used JSP for eons, but only now am I trying out Thymeleaf. I've found that for my application I need to add some adjustments to my application.properties file, and I've figured out the Spring Boot Thymeleaf settings to use.

But where can I find the authoritative, complete list of Thymeleaf settings Spring Boot uses by default? For example, I know that Spring Boot configures Thymeleaf to look in the /templates/ directory on the classpath for templates, and I know how to change that. But where is the default /templates/ configured? Is there a DefaultXXX class or a .properties file I can open to see how these are set and what values are used if I make no changes?

答案1

得分: 1

以下是翻译好的部分:


有一个属性设置列表在这里(https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html)- 你可以在那个页面上搜索“thymeleaf”来找到相关的部分。

示例:

键                           默认值                  描述
spring.thymeleaf.prefix  classpath:/templates/  构建URL时添加到视图名称前面的前缀。

这个示例显示了对于Thymeleaf的ClassLoaderTemplateResolver,使用了默认值/templates/(正如你所注意到的)。

我不知道这是否是权威且完整的列表 - 但根据我通常期望在非Spring Thymeleaf实现中设置的设置,它看起来相当全面。

更新

为了添加更多细节,看一下文档中的两个示例:

示例1: spring.thymeleaf.cache - 默认为true

对于这个值, 默认值在AbstractConfigurableTemplateResolver类中定义:

public static final boolean DEFAULT_CACHEABLE = true;

这里是GitHub上的源代码链接

示例2: spring.thymeleaf.prefix - 默认为classpath:/templates/

在这种情况下,有一个Spring类处理这个(和许多其他)默认值:org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties

最简单的方式可能是直接跳转到源代码并查看那里设置的默认值。

例如,前缀示例

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

还值得注意的是,示例1中的cache设置也在Spring的ThymeleafProperties类中被明确设置。

附加说明

还有用于反应性设置的属性:

spring.thymeleaf.reactive.max-chunk-size - 默认值为0B

这是在嵌套的Reactive类中设置的:

private DataSize maxChunkSize = DataSize.ofBytes(0);

上述位置仍然不是100%全面的。例如:

根据文档,spring.thymeleaf.enable-spring-el-compiler被设置为false。在源代码中,它隐式地为false:

private boolean enableSpringElCompiler;

但是这并没有告诉你如果选择将此属性设置为true,可能的编译器设置是什么(参见这里)。

关于属性文件的说明

我不知道是否有任何默认的属性文件或属性文件模板被用作这个Thymeleaf配置过程的一部分。我的理解是,如果你想自定义这些设置,并且你没有Spring属性文件,你必须自己创建它 - 但我可能是错的。

附加说明2

DEFAULT_PREFIX的示例为例:这由ThymeleafAutoConfiguration类处理(这里)。这将前缀设置在SpringResourceTemplateResolver类中,该类是Thymeleaf的Spring集成模块的一部分(源代码这里)。

resolver.setPrefix(this.properties.getPrefix());

这个解析器“使用Spring的资源解析机制来解析模板” - 这是我的研究的终点。

关于默认前缀与spring.thymeleaf.prefix提供的值之间的关系,我唯一看到直接联系的地方是这里,它调用了一个PropertyResolver方法来返回用户提供的属性值,否则返回默认值。但是这仅用于验证是否可以定位模板(而不是实际检索) - 所以这当然不是全部。这也是我的研究的终点。

英文:

There is a list of properties settings here - you can search that page for "thymeleaf" to find the relevant section.

Example:

Key                      Default Value          Description
spring.thymeleaf.prefix  classpath:/templates/  Prefix that gets prepended to view names when building a URL.

This example shows that for the Thymeleaf ClassLoaderTemplateResolver a default value of /templates/ is used (as you noted).

I don't know if it's The Authoritative, Complete List - but it looks to be reasonably comprehensive, based on the settings I would normally expect to be setting in a non-Spring Thymeleaf implementation.


Update

To add some more details, looking at two examples in the documentation:

Example 1: spring.thymeleaf.cache - defaults to true

For this value, the default is defined in the AbstractConfigurableTemplateResolver class:

public static final boolean DEFAULT_CACHEABLE = true;

Here is a link to the source code on GitHub.

Example 2: spring.thymeleaf.prefix - defaults to classpath:/templates/

In this case, there is a Spring class which handles this (and many other) defaults: org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties

It's probably easiest just to jump straight to the source and look at the defaults set there.

For example the prefix example:

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

It's also worth noting that the cache setting from example 1 above is also explicitly set in the Spring ThymeleafProperties class.

Additional Notes

There are also properties for reactive settings:

spring.thymeleaf.reactive.max-chunk-size - default is 0B

This is set in a nested Reactive class:

private DataSize maxChunkSize = DataSize.ofBytes(0);

The above locations are still not 100% comprehensive. For example:

spring.thymeleaf.enable-spring-el-compiler is set to false according to the documentation. And in the source code it is implicitly false:

private boolean enableSpringElCompiler;

But that does not tell you what possible compiler settings can be, if you choose to set this property to true (see here).

A Note on Properties Files

I am not aware of any default properties file or properties file template which is used as a part of this Thymeleaf configuration process. My understanding is that if you want to customize these settings, and you do not already have a Spring properties file, you have to create it yourself - but I may be mistaken.


Additional Notes 2

Taking the example of the DEFAULT_PREFIX: This is handled by the ThymeleafAutoConfiguration class (here). This sets the prefix in the SpringResourceTemplateResolver class, which is part of Thymeleaf's Spring integration module (source here).

resolver.setPrefix(this.properties.getPrefix());

This resolver "resolves templates using Spring's Resource Resolution mechanism" - which is where my research stalls.

Regarding the relationship between the default prefix and the value provided by spring.thymeleaf.prefix, the only location where I see a direct connection is here, which calls a PropertyResolver method to return the user-provided property value, or else the default value, otherwise. But again, this is only for verifying that a template can be located (rather than actually being retrieved) - so that is certainly not the full picture. Again, this is where my research stalls.

huangapple
  • 本文由 发表于 2020年8月24日 06:26:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63552623.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定