Spring boot freemarker模板路径的几种设置方式

首先给大家推荐一下我老师大神的人工智能教学网站。教学不仅零基础,通俗易懂,而且非常风趣幽默,还时不时有内涵黄段子!点这里可以跳转到网站

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中,你可以这样做:

@Configurationpublic 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;	}

本文为博主原创,欢迎转载,但转载请注明出处

点这里可以跳转到人工智能网站

发表评论