如何解決SpringBoot2.x版本對Velocity模板不支持的方案
背景
在springboot1.4.x版本之前對velocity的模板支持的是相當(dāng)好的,不止出于什么原因springboot從1.5.x以后停止了對velocity的支持,甚至在2.x版本中移除了和velocity的相關(guān)代碼。目前手上有些項目使用的是velocity模板引擎,同時也使用了springboot,現(xiàn)在想升級到springboot2.x,同時還想繼續(xù)使用velocity,怎么辦?springboot不支持,就自己想辦法支持下吧。 思路:把springboot早期版本的velocity支持單獨抽出一個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文件拷貝過來,整體代碼架構(gòu)如下圖:
直接編譯打包
接入使用:
在項目中直接添加如下依賴:
<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項目中使用velocity模板,歡迎有需要的小伙伴試用,使用過程中有問題可以直接反饋給我、
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解使用Mybatis-plus + velocity模板生成自定義的代碼
- c#基于NVelocity實現(xiàn)代碼生成
- SiteMesh如何結(jié)合Freemarker及velocity使用
- Vue中JS動畫與Velocity.js的結(jié)合使用
- SpringBoot與velocity的結(jié)合的示例代碼
- 聊聊JS動畫庫 Velocity.js的使用
- springMVC+velocity實現(xiàn)仿Datatables局部刷新分頁方法
- 詳解velocity模板使javaWeb的html+js實現(xiàn)模塊化
- Mybatis velocity腳本的使用教程詳解(推薦)
- JAVA velocity模板引擎使用實例
- html文件中jquery與velocity變量中的$沖突的解決方法
- Java 如何使用Velocity引擎生成代碼
相關(guān)文章
SpringBoot+Shiro+Redis+Mybatis-plus 實戰(zhàn)項目及問題小結(jié)
最近也是一直在保持學(xué)習(xí)課外拓展技術(shù),所以想自己做一個簡單小項目,于是就有了這個快速上手 Shiro 和 Redis 的小項目,說白了就是拿來練手調(diào)調(diào) API,然后做完后拿來總結(jié)的小項目,感興趣的朋友一起看看吧2021-04-04Struts2 ActionContext 中的數(shù)據(jù)詳解
這篇文章主要介紹了Struts2 ActionContext 中的數(shù)據(jù)詳解,需要的朋友可以參考下2016-07-07Java實現(xiàn)學(xué)生成績管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)學(xué)生成績管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-04-04SpringBoot 實戰(zhàn) 之 優(yōu)雅終止服務(wù)的方法
本篇文章主要介紹了SpringBoot 實戰(zhàn) 之 優(yōu)雅終止服務(wù)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05