springboot整合freemarker自动加载宏

springboot引入freemarker最大的问题,在于springboot的application.properties配置文件,不能覆盖所有的freemarker配置

如果freemarker有自定义宏应该怎样引入呢?

首先 application.properties增加配置(与freemarker.properties一样)

auto_import = _
auto_include = /layout/pageShow.html

然后增加FreemarkerConfig类,启动即可

/** * Freemarer 配置 * 增加自动注入和包含配置 * Created by 灰灰 on 2017/7/1. */@org.springframework.context.annotation.Configurationpublic class FreemarkerConfig {    private static Logger log = LoggerFactory.getLogger(FreemarkerConfig.class);    @Bean    public FreeMarkerConfigurer freeMarkerConfigurer(@Value("${auto_import}") String autoImport, @Value("${auto_include}") String autoInclude) {        FreeMarkerConfigurer config = new FreeMarkerConfigurer();        writerProperties(config);        Configuration configuration = null;        try {            configuration = config.createConfiguration();        } catch (IOException e) {            e.printStackTrace();        } catch (TemplateException e) {            e.printStackTrace();        }        setAutoImport(autoImport,configuration);        setAutoInclude(autoInclude,configuration);        config.setConfiguration(configuration);        return config;    }    @Autowired    private FreeMarkerProperties properties;

    private void writerProperties(FreeMarkerConfigurer config) {        config.setTemplateLoaderPaths(this.properties.getTemplateLoaderPath());        config.setPreferFileSystemAccess(this.properties.isPreferFileSystemAccess());        config.setDefaultEncoding(this.properties.getCharsetName());        Properties settings = new Properties();        settings.putAll(this.properties.getSettings());        config.setFreemarkerSettings(settings);    }    private void setAutoImport(String autoImport,Configuration configuration) {        if("_".equals(autoImport.trim())) {            return;        }        String[] imports = autoImport.split(";");        Map<String,String> importMap = new HashMap<String,String>(imports.length);        for (String s : imports) {            String[] keyValue = s.split("as");            if (keyValue.length != 2) {                log.error("freemarker配置auto_import格式不正确 ");                throw new RuntimeException("freemarker配置auto_import格式不正确");            }            importMap.put(keyValue[1].trim(),keyValue[0].trim());        }        configuration.setAutoImports(importMap);    }    private void setAutoInclude(final String autoInclude,Configuration configuration) {        if ("_".equals(autoInclude.trim())) {            return;        }        String[] includes = autoInclude.split(";");        for (String s : includes) {            System.out.println(s);        }        List list = new ArrayList<String>(Arrays.asList(includes));        configuration.setAutoIncludes(list);    }}


原文地址:https://www.cnblogs.com/rock-turf/p/11108006.html

时间: 2024-08-29 22:47:01

springboot整合freemarker自动加载宏的相关文章

springboot属性类自动加载配置文件中的值

springboot属性类自动加载配置文件中的值,如Person类加载在yml中配置的name,age等属性值,可以通过如下步骤获取: 类上添加@ConfigurationProperties注解,prefix为yml中配置的属性名称,要想属性类生效得加上@Component注解 如果想要在yml中有对应类的提示,还需要添加如下依赖: yml书写如下: 如果是properties文件,则书写如下: 在yml中如果值中有特殊字符,需要转义可以用单引号包裹,默认是双引号 如果仅仅为类中的某个属性值赋

VB.NET自动加载宏

VB.NET自动加载.xla文件 Public Function Loadxla(ByVal xlaPath As String, ByVal load As Boolean) As Boolean Dim loaded As Boolean = False Try If (File.Exists(xlaPath)) Then Dim o As Object o = XlApp.AddIns.Add(xlaPath, True) '定义成object,add by danielinbiti o.

访问修饰限定符的简单总结、final/abstruct/interface对类的限制、自动加载机制、序列化与反序列化【数据持久化和对象的序列化问题】、对象的拷贝(按引用是因为对象标识)和克隆(__clone方法中的this指向)

1.针对访问修饰限定符的理解只需要两点:(1)针对的是类的概念和访问代码的位置来确定是否能够访问(2)对访问修饰限定符的使用时只需要对该成员的使用场景注意即可[也就是内部,继承类,外部进行访问的权限] 不需要对内部进行太多理解[需要对php底层理解时进行理解] [重点][用途]通过访问修饰限定符将内部成员的权限合理的限制,然后再使用公共接口来调用这个基本服务,保证外部不能访问其内部的构件[这样既能够通过类内的设置,将内部的功能实现更好的限制,只有最外层的接口可以正常被访问到,而不了解内部的业务]

retrofit+rxjava+recyclerview+下拉刷新+自动加载更多

安卓开发过程中,网络请求与下拉刷新分页列表的控件几乎可以说是必不可少的,但是每次开发一款产品都要重新开发,肯定是不可取的,那么最好是可以自己整理一个开发框架,那么以后开发,直接引入项目即可 网络框架的封装,从httpclient,到xutils,再到volley,再到okhttp,每次整合都发现多多少少的不足,目前自己觉得最成熟的一个也就是retrofit+okhttp3+rxjava的组合,rxjava不懂的推荐看大神的深入浅出rxjava,retrofit的使用自己网上搜咯 下拉刷新列表的实

加载宏

Office VBA Addin加载宏是一种使用VBA编写的程序,它通过加载设置,可以随Office程序启动而自动加载运行,是制作Excel自定义函数.Office菜单和功能区按钮.添加常用辅助功能的常用载体. 在Excel.PPT中都可以编制加载宏文件,Word则可以通过模板文件进行加载.根据版本和程序的不同,加载宏的文件类型也稍有区别,在2003版本中,Excel的加载宏扩展名是xla.PPT的加载宏扩展名是ppa:2007/2010版本中,Excel的加载宏扩展名是xlam,PPT的加载宏

springboot整合freemarker

前后端分离现在越来越多,如何有效的使用springboot来整合我们的页面是一个很重要的问题. springboot整合freemarker有以下几个步骤,也总结下我所犯的错误: 1.加依赖: 2.配置文件修改: 3.在templates下面编写后缀为ftl的页面: 4.错误出现:404问题: (1)检查是@RestController还是@Controller,如果要返回页面必须用@Controller (2)这次问题出现的很粗心:(漏掉一个小点) 原文地址:https://www.cnblo

PHP 命名空间与自动加载机制

include 和 require 是PHP中引入文件的两个基本方法.在小规模开发中直接使用 include 和 require 没哟什么不妥,但在大型项目中会造成大量的 include 和 require 堆积.这样的代码既不优雅,执行效率也很低,而且维护起来也相当困难. 为了解决这个问题,部分框架会给出一个引入文件的配置清单,在对象初始化的时候把需要的文件引入.但这只是让代码变得更简洁了一些,引入的效果仍然是差强人意.PHP5 之后,随着 PHP 面向对象支持的完善,__autoload 函

Yii2的深入学习--自动加载机制

Yii2 的自动加载分两部分,一部分是 Composer 的自动加载机制,另一部分是 Yii2 框架自身的自动加载机制. Composer自动加载 对于库的自动加载信息,Composer 生成了一个 vendor/autoload.php 文件.你可以简单的引入这个文件,你会得到一个自动加载的支持. 在之前的文章,入口文件的介绍中,我们可以看到如下内容: // 引入 vendor 中的 autoload.php 文件,会基于 composer 的机制自动加载类 require(__DIR__ .

ThinkPHP 3.2.3 自动加载公共函数文件的方法

方法一.加载默认的公共函数文件 在 ThinkPHP 3.2.3 中,默认的公共函数文件位于公共模块 ./Application/Common 下,访问所有的模块之前都会首先加载公共模块下面的配置文件(Conf/config.php)和公共函数文件(Common/function.php),即默认的公共函数文件为 ./Application/Common/Common/function.php. 例如,在 ./Application/Common/Common 下新建 function.php,