集成freemarker,很簡單很快捷。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
注意注意,這里我是沒有寫版本號的,原因看第一篇,小伙伴們注意下,可以通過集成springboo為父模塊,這樣就可以不用添加版本號,也可以通過maven的公共版本管理的方法,這樣可以不用添加版本號,只需要在管理的地方統一管理就好了。
我這里用的maven分模塊開發,在父模塊中添加了。最后就是自己手動搜索包,然后找到對應的版本號,添加上去吧。這里提下
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.4.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
記住確認下包有沒有進來。
添加完freemarker的依賴后,添加模板頁,進行測試,可以運行第一章的最后一個hello例子,試試。
不需要進行任何的配置,可以查看下他源代碼試試。因為前幾天擴展他freemarker的配置時有看到一點,順便說下咯。
首先,為什么不需要任何的配置,只要在resources下添加templates,他就能默認匹配到對應的模板頁,這是主要的問題。
springboot,他有默認自帶的配置文件,在這個配置文件中他集成了常用框架所需要的常用配置,所以我們只需要根據他的配置做開發就好,或者是在resources下添加application.properties 文件,然后設置對應項就好,這個默認配置文件我以前在jar包找到過,這陣子找了很久都沒找到,但是沒事http://www.tuicool.com/articles/veUjQba 可以去這里看下他默認配置。
這是第一步,他會去讀取application.properties文件和默認配置文件,然后加載對應的配置到bean中。
freemarker在springboot中的配置bean的位置
自動配置包
所以他默認會去匹配templates文件夾下以.ftl 結尾的bean.當然還有其他的配置項在其他的地方。
到這里freemarker 的集成基本入門就到這里。
接下會追加一個前幾天寫的freemarker的擴展配置。
由於springboot中並沒有集成freemarker的自動導入和自動包含屬性,所以只能自己加載進入相關的配置,這里貼寫代碼和大概的解釋下。
首先在配置文件中加入相關配置
# 如果auto_import和auto_include 空值的話請賦值 _ 下划線 auto_import = test.ftl as t;test1.ftl as t1 auto_include = test.ftl
為什么下划線呢,因為空值后面的注入會報錯,所以就硬性規則為下划線,當時做的急,就這樣了。
一個自定義異常類,用於捕獲自定義加載時的拋出異常,其他幾個異常類就不貼了,不然太多了,可以簡單的實現下。
public class ConfigException extends BaseException implements RetCodeSupport { private final static String code = RetCode.FAIL_CONFIG; public ConfigException(String message) { super(message); } public ConfigException(String message, Throwable throwable) { super(message,message); } @Override public String getRetCode() { return code; } }
重點是freemarker的加載bean,先大概的的看下,后面解釋下。
/** * Freemarer 配置 * 增加自動注入和包含配置 * Created by 灰灰 on 2017/7/1. */ @org.springframework.context.annotation.Configuration public 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) { throw new ConfigException("freemarker配置bean,IO異常",e); } catch (TemplateException e) { throw new ConfigException("freemarker配置bean異常",e); } 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 ConfigException("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); } }
@Configuration 本來是這樣的注解的,但是用於下面有freemarker的同名類,所以這里得這樣寫了。
這里首先注入配置文件里需要導入和包含的模板路徑,然后對模板路徑做相對於的格式化,取出對應的信息,然后填充到集合中。通過注入獲取到springboot注入的基本信息,然后把這些配置信息都填充的FreeMarkerConfigurer 里,返回return就好了,這里用到了@bean,他會交給spring框架去管理。
大概就這樣子。