简体   繁体   中英

Freemarker Set checkTemplateLocation via Java

I have a Spring Boot web application. I am using Freemarker as my ViewResolver and I configure everything via 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. 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.

How do I set this setting via a Java configuration?

I think you are confusing FreeMarker Configuration settings, which are unrelated to Spring, with Spring's configuration properties. spring.freemarker.checkTemplateLocation is a Spring-specific thing, that you can, for example, put into your application.properties . FreeMarker Configuration settings are defined by FreeMarker, and note that they aren't called "properties", but "settings"; maybe the confusion comes from that you are using a java.util.Properties object to specify them.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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