springboot整合freemarker以及auto_import、auto_include配置使用

1.application.properties配置:


    
    
  1. # FREEMARKER (FreeMarkerAutoConfiguration)
  2. spring.freemarker.allow-request-override=false
  3. spring.freemarker.allow-session-override=false
  4. spring.freemarker.cache=false
  5. spring.freemarker.charset=UTF-8
  6. spring.freemarker.check-template-location=true
  7. spring.freemarker.content-type=text/html
  8. spring.freemarker.enabled=true
  9. spring.freemarker.expose-request-attributes=true
  10. spring.freemarker.expose-session-attributes=true
  11. spring.freemarker.expose-spring-macro-helpers=true
  12. spring.freemarker.prefer-file-system-access=true
  13. spring.freemarker.suffix=.html
  14. spring.freemarker.template-loader-path=classpath:/templates/
  15. spring.freemarker.settings.template_update_delay=0
  16. spring.freemarker.settings.default_encoding=UTF-8
  17. spring.freemarker.settings.classic_compatible=true
  18. spring.freemarker.settings.date_format=yyyy-MM-dd
  19. spring.freemarker.settings.time_format=HH:mm:ss
  20. spring.freemarker.settings.datetime_format=yyyy-MM-dd HH:mm:ss
  21. spring.freemarker.request-context-attribute=request
  22. spring.freemarker.order=1
  23. auto_import=ftl/spring.ftl as s
  24. auto_include=

2.spring.ftl存放位置:


    
    
  1. templates/ftl/spring.ftl
  2. 内容: <#assign base = request.contextPath />

3.freemarkerconfig配置:


    
    
  1. @org.springframework.context.annotation.Configuration
  2. public class FreemarkerConfig {
  3. private static Logger log = LoggerFactory.getLogger(FreemarkerConfig.class);
  4. @Bean
  5. public FreeMarkerConfigurer freeMarkerConfigurer(@Value("${auto_import}") String autoImport,@Value("${auto_include}") String autoInclude) throws Exception {
  6. FreeMarkerConfigurer config = new FreeMarkerConfigurer();
  7. writerProperties(config);
  8. Configuration configuration = null;
  9. try {
  10. configuration = config.createConfiguration();
  11. } catch (IOException e) {
  12. throw new ConfigException( "freemarker配置bean,IO异常",e);
  13. } catch (TemplateException e) {
  14. throw new ConfigException( "freemarker配置bean异常",e);
  15. }
  16. setAutoImport(autoImport,configuration);
  17. setAutoInclude(autoInclude,configuration);
  18. config.setConfiguration(configuration);
  19. return config;
  20. }
  21. @Autowired
  22. private FreeMarkerProperties properties;
  23. private void writerProperties(FreeMarkerConfigurer config) {
  24. config.setTemplateLoaderPaths( this.properties.getTemplateLoaderPath());
  25. config.setPreferFileSystemAccess( this.properties.isPreferFileSystemAccess());
  26. config.setDefaultEncoding( this.properties.getCharsetName());
  27. Properties settings = new Properties();
  28. settings.putAll( this.properties.getSettings());
  29. config.setFreemarkerSettings(settings);
  30. }
  31. private void setAutoImport(String autoImport,Configuration configuration) throws Exception {
  32. if( "_".equals(autoImport.trim())) {
  33. return;
  34. }
  35. String[] imports = autoImport.split( ";");
  36. Map<String,String> importMap = new HashMap<String,String>(imports.length);
  37. for (String s : imports) {
  38. String[] keyValue = s.split( "as");
  39. if (keyValue.length != 2) {
  40. log.error( "freemarker配置auto_import格式不正确 ");
  41. throw new ConfigException( "freemarker配置auto_import格式不正确");
  42. }
  43. importMap.put(keyValue[ 1].trim(),keyValue[ 0].trim());
  44. }
  45. configuration.setAutoImports(importMap);
  46. }
  47. private void setAutoInclude(final String autoInclude,Configuration configuration) {
  48. if ( "_".equals(autoInclude.trim())) {
  49. return;
  50. }
  51. String[] includes = autoInclude.split( ";");
  52. for (String s : includes) {
  53. System.out.println(s);
  54. }
  55. List list = new ArrayList<String>(Arrays.asList(includes));
  56. configuration.setAutoIncludes(list);
  57. }
  58. }

4.页面引用:

${s.base}或者使用${request.contextPath}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值