Spring boot freemarker模板路徑的幾種設定方式
阿新 • • 發佈:2019-02-10
spring boot中使用freemarker模板引擎技術,spring boot中提供了一些預設的配置。預設配置如下所示:
本文只探討freemarker中模板路徑的設定方式,其他配置,請注意檢視後續文章。
# FREEMARKER (FreeMarkerAutoConfiguration)
spring.freemarker.allow-request-override=false # Set whether HttpServletRequest attributes are allowed to override (hide) controller generated model attributes of the same name.
spring.freemarker.allow-session-override=false # Set whether HttpSession attributes are allowed to override (hide) controller generated model attributes of the same name.
spring.freemarker.cache=false # Enable template caching.
spring.freemarker.charset=UTF-8 # Template encoding.
spring.freemarker.check-template-location=true # Check that the templates location exists.
spring.freemarker.content-type=text/html # Content-Type value.
spring.freemarker.enabled=true # Enable MVC view resolution for this technology.
spring.freemarker.expose-request-attributes=false # Set whether all request attributes should be added to the model prior to merging with the template.
spring.freemarker.expose-session-attributes=false # Set whether all HttpSession attributes should be added to the model prior to merging with the template.
spring.freemarker.expose-spring-macro-helpers=true # Set whether to expose a RequestContext for use by Spring's macro library, under the name "springMacroRequestContext".
spring.freemarker.prefer-file-system-access=true # Prefer file system access for template loading. File system access enables hot detection of template changes.
spring.freemarker.prefix= # Prefix that gets prepended to view names when building a URL.
spring.freemarker.request-context-attribute= # Name of the RequestContext attribute for all views.
spring.freemarker.settings.*= # Well-known FreeMarker keys which will be passed to FreeMarker's Configuration.
spring.freemarker.suffix= # Suffix that gets appended to view names when building a URL.
spring.freemarker.template-loader-path=classpath:/templates/ # Comma-separated list of template paths.
spring.freemarker.view-names= # White list of view names that can be resolved.
如上所示,您可以在spring boot的預設配置檔案中設定模板路徑(spring boot預設讀取的配置檔案為:application.properties,當然還有其他的讀取方式,如果想了解這方面的文章,請檢視後續文章)
spring.freemarker.template-loader-path=classpath:/templates/ # Comma-separated list of template paths.
此種配置,只能讀取classpath下的路徑。如果想要讀取其他路徑則要重寫freemarker的預設配置,在spring boot中,你可以這樣做:
@Configuration
public class TemplateLoader extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
resolver.setCache(true);
resolver.setPrefix("");
resolver.setSuffix(".ftl");
resolver.setContentType("text/html; charset=UTF-8");
return resolver;
}
@Bean
public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPaths("file:絕對路徑","http://www.xxx.com/");
configurer.setDefaultEncoding("UTF-8");
return configurer;
}
}
如程式碼所示,在此處,你可以設定多個freemarker路徑,當然越靠前的路徑權重越高,系統會在讀取第一個失敗的情況下,會繼續讀取第二個路徑。
在此處,你可以有三種方式讀取模板路徑
第一種:classpath:/static/
第二種:file:絕對路徑
第三種:http://www.xxx.com
其中第一種和第二種是統一型別,但是,第一種只能讀取系統配置檔案路徑,第二種只能讀取絕對路徑。第三種則可以讀取ftp檔案
關於setTemplateLoaderPaths的方法,在原始碼中的解釋,我們可以看一下:
/**
* Set multiple Freemarker template loader paths via Spring resource locations.
* <p>When populated via a String, standard URLs like "file:" and "classpath:"
* pseudo URLs are supported, as understood by ResourceEditor. Allows for
* relative paths when running in an ApplicationContext.
* <p>Will define a path for the default FreeMarker template loader.
* If a specified resource cannot be resolved to a {@code java.io.File},
* a generic SpringTemplateLoader will be used, without modification detection.
* <p>To enforce the use of SpringTemplateLoader, i.e. to not resolve a path
* as file system resource in any case, turn off the "preferFileSystemAccess"
* flag. See the latter's javadoc for details.
* <p>If you wish to specify your own list of TemplateLoaders, do not set this
* property and instead use {@code setTemplateLoaders(List templateLoaders)}
* @see org.springframework.core.io.ResourceEditor
* @see org.springframework.context.ApplicationContext#getResource
* @see freemarker.template.Configuration#setDirectoryForTemplateLoading
* @see SpringTemplateLoader
*/
public void setTemplateLoaderPaths(String... templateLoaderPaths) {
this.templateLoaderPaths = templateLoaderPaths;
}
本文為博主原創,歡迎轉載,但轉載請註明出處。