java中Spring boot如何设置freemarker模板路径

 时间:2024-10-14 03:09:46

1、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.

java中Spring boot如何设置freemarker模板路径

2、如上所示,您可以在spring boot的默认配置文件中设置模板路径(spring boot默认读取的配置文件为:application.properties,当然还有其他的读取方式,如果想了解这方面的文章,请查看后续文章)图中代码如下(图片只是为了查看方便):spring.freemarker.template-loader-path=classpath:/templates/ # Comma-separated list of template paths.

java中Spring boot如何设置freemarker模板路径

3、此种配置,只能读取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; }}

java中Spring boot如何设置freemarker模板路径

4、如代码所示,在此处,你可以设置多个freemarker路径,当然越靠前的路径权重越高,系统会在读取第一个失败的情况下,会继续读取第二个路径。在此处,你可以有三种方式读取模板路径第一种:classpath:/static/第二种:file:绝对路径第三种:http://www.xxx.com其中第一种和第二种是统一类型,但是,第一种只能读取系统配置文件路径,第二种只能读取绝对路径。第三种则可以读取ftp文件关于setTemplateLoaderPaths的方法,在源码中的解释,我们可以看一下:图中代码如下(图片只是为了查看方便):关于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; }

java中Spring boot如何设置freemarker模板路径
  • oracle安装教程
  • 使用PLSQL怎样将oracle中的数据导出到cvs中
  • 如何用sqlserver语句查看硬盘分区
  • Oracle如何给新建的用户授权
  • idea 和eclipse的debug调试快捷键对比
  • 热门搜索
    关于生命的手抄报 新学期新征程手抄报 班级公约手抄报 手抄报疫情 祖国建设新成就手抄报 科技手抄报内容大全 健康成长手抄报 关于春节的手抄报图片 做一个有道德的人手抄报 小手拉大手手抄报