1. 程式人生 > >springboot整合freemarker以及auto_import、auto_include配置使用

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

1.application.properties配置:

# FREEMARKER (FreeMarkerAutoConfiguration) 
spring.freemarker.allow-request-override=false
spring.freemarker.allow-session-override=false
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.enabled=true
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.expose-spring-macro-helpers=true
spring.freemarker.prefer-file-system-access=true
spring.freemarker.suffix=.html
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.settings.template_update_delay=0
spring.freemarker.settings.default_encoding=UTF-8
spring.freemarker.settings.classic_compatible=true
spring.freemarker.settings.date_format=yyyy-MM-dd
spring.freemarker.settings.time_format=HH:mm:ss
spring.freemarker.settings.datetime_format=yyyy-MM-dd HH:mm:ss
spring.freemarker.request-context-attribute=request
spring.freemarker.order=1
auto_import=ftl/spring.ftl as s
auto_include=

2.spring.ftl存放位置:

templates/ftl/spring.ftl
內容:<#assign base = request.contextPath />

3.freemarkerconfig配置:

@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) throws Exception {
        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) throws Exception {
        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);
    }
}

4.頁面引用:

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


相關推薦

springboot整合freemarker以及auto_importauto_include配置使用

1.application.properties配置:# FREEMARKER (FreeMarkerAutoConfiguration) spring.freemarker.allow-request-override=false spring.freemarker.al

SpringBoot整合Mybatis 以及 mybatis自動生成程式碼配置

1、springboot的配置檔案application.yml #埠 server: port: 8080 #模板頁面 #註釋的部分是Thymeleaf預設的配置,如有其它需求可以自行更改 spring: thymeleaf: cac

springboot整合mybatis及封裝curd操作-配置文件

enabled () init tps github mys tde oos maven 1 配置文件 application.properties #server server.port=8090 server.address=127.0.0.1 server.ses

SpringBoot整合Freemarker+Mybatis

strong res esp ngs wire hid names 插件 sql 開發工具 , 開始 新建工程 .選擇Spring Initializr 下一步 下一步,選擇需要的組件 ..改一下工程名,Finish ..目錄結構 首先,修改po

springboot整合freemarker

後綴 分享圖片 template 有效 分離 com 配置 技術 mar 前後端分離現在越來越多,如何有效的使用springboot來整合我們的頁面是一個很重要的問題。 springboot整合freemarker有以下幾個步驟,也總結下我所犯的錯誤: 1、加依賴: 2、

springboot整合freemarker和thymeleaf

整合freemarker <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarker --> <dependency> <g

SpringBoot整合Freemarker模板技術

    FreeMarker 是一款 模板引擎: 即一種基於模板和要改變的資料,並用來生成輸出文字(HTML網頁,電子郵件,配置檔案,原始碼等)的通用工具。它不是面向終端使用者的,而是一個Java類庫,是一款程式設計師可以嵌入他們所開發產品的元件。  模板編寫為FreeMarke

springboot整合freemarker檢視層

一、在pom檔案中引入freemarker需要的依賴: <!-- 引入freemarker的依賴包 --> <dependency> <groupId>org.springframework.boot</groupId>

springboot--整合Freemarker

在pom檔案中新增freemarker的啟動器 <!--freemarker啟動器--> <dependency> <groupId>org.springframework.boot</gro

SpringBoot整合mybatis以及使用mybtis逆向生成外掛

引入依賴 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot

Springboot整合freemarker和jpa

首先開啟idea新建一個springboot專案:file-new project 然後點選next 分別選中web ->web TemplateEngine-> freemarker SQL->jpa 然後next ->finish pom檔案如下`<

springboot 整合kafka系列 一springboot整合kafka生產者

1、新建springboot腳手架工程,pom檔案如下,其中引入了kafka需要的依賴,注意這裡的kafka版本號需要和之前安裝的kafka版本一致,要不然會有問題 <?xml version="1.0" encoding="UTF-8"?> <proje

SpringBoot-整合freemarker例項

一、前言 FreeMarker是一個模板引擎,一個基於模板生成文字輸出的通用工具,使用純Java編寫 FreeMarker被設計用來生成HTML Web頁面,特別是基於MVC模式的應用程式 雖然FreeMarker具有一些程式設計的能力,但通常由Java程式準備要顯示的

springboot整合redis(叢集) yml配置及demo

一、加入maven配置 <!-- 整合redis --> <dependency> <groupId>org.springframework.boot</groupId>

springboot排除資料庫以及mongo的自動配置

測試某個功能時,新建了一個專案,因為要保證依賴相同,所以直接把依賴pom檔案直接複製過來,但是依賴中有mongo的依賴,導致了springboot自動配置了mongo,但是並沒有配置mongo的資訊,導致初始化的連線報錯,解決辦法,直接排除mongo的自動配置。 資料庫的DataSo

SpringBoot 整合Freemarker過程中出現的問題 ?

1、idea和eclipse如何建立spring boot專案?      idea:            1)在官網上建立好,用idea開啟,new --- project   --- existing resource --- >next ...         

springboot 整合redis 以及redis的簡單使用

redis是一種支援Key-Value等多種資料結構的儲存系統,用於快取還是很方便的,  springboot整合redis 也很簡單。   首先 在 pom檔案裡面 加入redis的相關依賴 :   <!-- Redis客戶端 --><dependen

SpringBoot整合shiro(一)基礎配置

公司專案採用的spring-boot框架。在做使用者許可權功能的時候準備採用shiro許可權框架。前面也考慮過spring家族的spring security安全框架。但是經過網上查詢對比最終選擇了shiro。因為shiro含有基本的安全控制功能,並且配置更為簡

學習Docker之Docker初體驗---SpringBoot整合Docker的部署釋出與應用

準備工作 會一點springboot 不會沒關係,花十幾分鍾補一下Quick-SpringBoot 會一點Maven 不會沒關係,花幾分鐘補一下Maven的快速應用 會一點Linux命令 不會沒關係,花十幾分再補一下Linux菜鳥教程 一臺

JavaEE開發之SpringBoot整合MyBatis以及Thymeleaf模板引擎

上篇部落格我們聊了《》,從上篇部落格的內容我們不難看出SpringBoot的便捷。本篇部落格我們繼續在上篇部落格的基礎上來看一下SpringBoot是如何引入和使用MyBatis和Thymeleaf的。在之前的部落格中我們提到過Hibernate,今天部落格所引入的Mybatis所扮演的角色與Hibernat