简体   繁体   English

Freemarker通过Java设置checkTemplateLocation

[英]Freemarker Set checkTemplateLocation via Java

I have a Spring Boot web application. 我有一个Spring Boot Web应用程序。 I am using Freemarker as my ViewResolver and I configure everything via java. 我将Freemarker用作ViewResolver,并通过Java配置了所有内容。 I am getting a warning in my log file: 我在日志文件中收到警告:

2018-02-07 19:00:28,592 WARN  o.s.b.a.f.FreeMarkerAutoConfiguration - 
Cannot find template location(s): [classpath:/templates/] 
(please add some templates, check your FreeMarker configuration, or set spring.freemarker.checkTemplateLocation=false)

I have my templates in an external location so the warning is correct. 我的模板位于外部位置,因此警告是正确的。

I have my config set up like this: 我有这样的配置设置:

@Bean 
public FreeMarkerConfigurer freemarkerConfig() { 
    FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); 
    freeMarkerConfigurer.setTemplateLoaderPath(freeMarkerConfiguration.getBasePath());
    return freeMarkerConfigurer; 
}

The templateLoaderPath is an external file: location and does have templates. templateLoaderPath是一个外部文件:location,并且具有模板。 In fact, everything is working fine, I just want to get the warning out of my log file, but has taken me down a rabbit hole that I now need to figure out. 实际上,一切工作正常,我只是想从日志文件中消除警告,但是已经使我陷入困境,现在我需要弄清楚这一点。 So I added that property to my configuration like this: 因此,我将该属性添加到我的配置中,如下所示:

@Bean 
public FreeMarkerConfigurer freemarkerConfig() { 
    FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); 
    Properties props = new Properties();
    props.setProperty("checkTemplateLocation", "false");
    freeMarkerConfigurer.setTemplateLoaderPath(freeMarkerConfiguration.getBasePath());
    freeMarkerConfigurer.setFreemarkerSettings(props);
    return freeMarkerConfigurer; 
}

this is throwing an error: 这会引发错误:

 Unknown FreeMarker configuration setting: "checkTemplateLocation"

I have tried "freemarker.checkTemplateLocation" and "spring.freemarker.checkTemplateLocation" and neither have worked. 我已经尝试过“ freemarker.checkTemplateLocation”和“ spring.freemarker.checkTemplateLocation”,但都没有起作用。

How do I set this setting via a Java configuration? 如何通过Java配置设置此设置?

I think you are confusing FreeMarker Configuration settings, which are unrelated to Spring, with Spring's configuration properties. 我认为您将与Spring无关的FreeMarker Configuration设置与Spring的配置属性混淆了。 spring.freemarker.checkTemplateLocation is a Spring-specific thing, that you can, for example, put into your application.properties . spring.freemarker.checkTemplateLocation是特定于Spring的东西,例如,您可以将其放入application.properties FreeMarker Configuration settings are defined by FreeMarker, and note that they aren't called "properties", but "settings"; FreeMarker配置设置由FreeMarker定义,请注意,它们不是“属性”,而是“设置”。 maybe the confusion comes from that you are using a java.util.Properties object to specify them. 可能是由于您使用java.util.Properties对象指定它们而引起的困惑。

Though of course the best would be to get rid of the cause of the warning... does your setTemplateLoaderPath call happen later than the WARN logged? 尽管当然最好的方法是消除警告的原因... setTemplateLoaderPath调用发生在WARN记录之后吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM