如何解決SpringBoot2.x版本對(duì)Velocity模板不支持的方案
背景
在springboot1.4.x版本之前對(duì)velocity的模板支持的是相當(dāng)好的,不止出于什么原因springboot從1.5.x以后停止了對(duì)velocity的支持,甚至在2.x版本中移除了和velocity的相關(guān)代碼。目前手上有些項(xiàng)目使用的是velocity模板引擎,同時(shí)也使用了springboot,現(xiàn)在想升級(jí)到springboot2.x,同時(shí)還想繼續(xù)使用velocity,怎么辦?springboot不支持,就自己想辦法支持下吧。 思路:把springboot早期版本的velocity支持單獨(dú)抽出一個(gè)jar。
步驟1:
git clone https://github.com/spring-projects/spring-framework.git
切換到 4.3.2.RELEASE 版本;拷貝org.springframework.ui.velocity和org.springframework.web.servlet.view.velocity 模塊下velocity的相關(guān)代碼;
步驟2:
git clone https://github.com/spring-projects/spring-boot.git
切換到v1.4.0.RELEASE;拷貝:org.springframework.boot.autoconfigure.velocity模塊下 velocity的相關(guān)代碼;
由于spring5.x及springboot2.x移除了velocity相關(guān)的代碼及配置,還要把spring.vm文件拷貝過(guò)來(lái),整體代碼架構(gòu)如下圖:
直接編譯打包
接入使用:
在項(xiàng)目中直接添加如下依賴:
<dependency> <groupId>com.dianwoda.velocity</groupId> <artifactId>spring-boot-velocity-starter</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency>
并添加如下配置:
spring.velocity.charset=UTF-8 spring.velocity.properties.input.encoding=UTF-8 spring.velocity.properties.output.encoding=UTF-8 spring.velocity.resourceLoaderPath=classpath:/templates/ spring.velocity.suffix=.vm spring.velocity.toolbox-config-location=/WEB-INF/toolbox.xml
在spring.xml中添加視圖解析配置:
<!-- 設(shè)置視圖解析工具 --> <bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver"> <property name="cache" value="false"/> <property name="layoutUrl" value="layout/layout.vm"/> <property name="prefix" value="/templates/"/> <property name="suffix" value=".vm"/> <property name="exposeSpringMacroHelpers" value="true"/> <property name="contentType" value="text/html;charset=UTF-8"/> <property name="viewClass" value="org.springframework.web.servlet.view.velocity.VelocityLayoutView"/> </bean>
按照上述配置即可在springboot2.x項(xiàng)目中使用velocity模板,歡迎有需要的小伙伴試用,使用過(guò)程中有問(wèn)題可以直接反饋給我、
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解使用Mybatis-plus + velocity模板生成自定義的代碼
- c#基于NVelocity實(shí)現(xiàn)代碼生成
- SiteMesh如何結(jié)合Freemarker及velocity使用
- Vue中JS動(dòng)畫與Velocity.js的結(jié)合使用
- SpringBoot與velocity的結(jié)合的示例代碼
- 聊聊JS動(dòng)畫庫(kù) Velocity.js的使用
- springMVC+velocity實(shí)現(xiàn)仿Datatables局部刷新分頁(yè)方法
- 詳解velocity模板使javaWeb的html+js實(shí)現(xiàn)模塊化
- Mybatis velocity腳本的使用教程詳解(推薦)
- JAVA velocity模板引擎使用實(shí)例
- html文件中jquery與velocity變量中的$沖突的解決方法
- Java 如何使用Velocity引擎生成代碼
相關(guān)文章
SpringBoot+Shiro+Redis+Mybatis-plus 實(shí)戰(zhàn)項(xiàng)目及問(wèn)題小結(jié)
最近也是一直在保持學(xué)習(xí)課外拓展技術(shù),所以想自己做一個(gè)簡(jiǎn)單小項(xiàng)目,于是就有了這個(gè)快速上手 Shiro 和 Redis 的小項(xiàng)目,說(shuō)白了就是拿來(lái)練手調(diào)調(diào) API,然后做完后拿來(lái)總結(jié)的小項(xiàng)目,感興趣的朋友一起看看吧2021-04-04Struts2 ActionContext 中的數(shù)據(jù)詳解
這篇文章主要介紹了Struts2 ActionContext 中的數(shù)據(jù)詳解,需要的朋友可以參考下2016-07-07Java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04解析SpringBoot項(xiàng)目開(kāi)發(fā)之Gzip壓縮過(guò)程
這篇文章主要介紹了SpringBoot項(xiàng)目開(kāi)發(fā)之Gzip壓縮過(guò)程,本文給大家分享幾種Gzip壓縮方式,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07SpringBoot 實(shí)戰(zhàn) 之 優(yōu)雅終止服務(wù)的方法
本篇文章主要介紹了SpringBoot 實(shí)戰(zhàn) 之 優(yōu)雅終止服務(wù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05識(shí)別率很高的java文字識(shí)別技術(shù)
這篇文章主要為大家詳細(xì)介紹了識(shí)別率很高的java文字識(shí)別技術(shù),親測(cè),希望對(duì)大家有幫助,感興趣的小伙伴們可以參考一下2016-08-08