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

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

 

 

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

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Spring Boot是一个用于构建Java应用的开发框架,它提供了简化的配置和快速的开发方式FreeMarker是一个Java模板引擎,它可以在模板中嵌入动态内容,生成动态的网页或其他文本格式。MyBatis是一个持久化框架,它可以与数据库进行交互,实现数据的增删改查功能。MySQL是一个开源的关系型数据库管理系统,它提供了高效可靠的数据存储和检索。 结合这些技术,我们可以开发一个商城系统。首先,使用Spring Boot框架来搭建项目的骨架,包括配置数据库连接、Web服务器等。然后,通过MyBatis框架来定义商品、订单等数据表的实体对象,并编写相应的数据访问层接口和实现。使用MySQL数据库来存储这些数据。 接下来,使用FreeMarker模板引擎来设计和生成商城的前端页面。我们可以通过在模板中嵌入动态的数据,展示商品列表、订单信息等内容。同时,可以使用FreeMarker的标签和语法来实现页面的逻辑控制、表单验证等功能。 在商城系统中,用户可以浏览商品、购买商品、查看订单等。我们可以通过设计相应的控制器和路由来实现这些功能,并在前端页面中添加相应的链接和按钮。用户购买商品时,可以将订单信息保存到数据库中,以便后续的订单管理和发货等操作。 总之,使用Spring BootFreeMarker、MyBatis和MySQL等技术,我们可以很方便地开发一个商城系统。通过这些技术的结合,我们可以实现商城的后台数据管理和前台页面展示,为用户提供便捷的购物体验。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值