背景
在springboot1.4.x版本之前对velocity的模板支持的是相当好的,不止出于什么原因springboot从1.5.x以后停止了对velocity的支持,甚至在2.x版本中移除了和velocity的相关代码。目前手上有些项目使用的是velocity模板引擎,同时也使用了springboot,现在想升级到springboot2.x,同时还想继续使用velocity,怎么办?springboot不支持,就自己想办法支持下吧。 思路:把springboot早期版本的velocity支持单独抽出一个jar。
步骤1:
|
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的相关代码;
步骤2:
|
1
|
git clone https://github.com/spring-projects/spring-boot.git
|
切换到v1.4.0.release;拷贝:org.springframework.boot.autoconfigure.velocity模块下 velocity的相关代码;
由于spring5.x及springboot2.x移除了velocity相关的代码及配置,还要把spring.vm文件拷贝过来,整体代码架构如下图:
直接编译打包
接入使用:
在项目中直接添加如下依赖:
|
1
2
3
4
5
|
<dependency>
<groupid>com.dianwoda.velocity</groupid>
<artifactid>spring-boot-velocity-starter</artifactid>
<version>1.0.0-snapshot</version>
</dependency>
|
并添加如下配置:
|
1
2
3
4
5
6
|
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中添加视图解析配置:
|
1
2
3
4
5
6
7
8
9
10
|
<!-- 设置视图解析工具 -->
<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模板,欢迎有需要的小伙伴试用,使用过程中有问题可以直接反馈给我、
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://tech.dianwoda.com/2018/12/01/jie-jue-springboot2-xban-ben-dui-velocitymo-ban-bu-zhi-chi-de-fang-an/
相关文章
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-25 83
-
2025-05-25 66
-
2025-05-26 79
-
关于安装linux redhat后无法使用yum命令安装gcc-c++问题的解决过程
2025-05-27 54 -
2025-05-29 88


