经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
如何解决SpringBoot2.x版本对Velocity模板不支持的方案
来源:jb51  时间:2018/12/3 14:32:23  对本文有异议

背景

在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. <dependency>
  2. <groupId>com.dianwoda.velocity</groupId>
  3. <artifactId>spring-boot-velocity-starter</artifactId>
  4. <version>1.0.0-SNAPSHOT</version>
  5. </dependency>

并添加如下配置:

  1. spring.velocity.charset=UTF-8
  2. spring.velocity.properties.input.encoding=UTF-8
  3. spring.velocity.properties.output.encoding=UTF-8
  4. spring.velocity.resourceLoaderPath=classpath:/templates/
  5. spring.velocity.suffix=.vm
  6. spring.velocity.toolbox-config-location=/WEB-INF/toolbox.xml

在spring.xml中添加视图解析配置:

  1. <!-- 设置视图解析工具 -->
  2. <bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
  3. <property name="cache" value="false"/>
  4. <property name="layoutUrl" value="layout/layout.vm"/>
  5. <property name="prefix" value="/templates/"/>
  6. <property name="suffix" value=".vm"/>
  7. <property name="exposeSpringMacroHelpers" value="true"/>
  8. <property name="contentType" value="text/html;charset=UTF-8"/>
  9. <property name="viewClass" value="org.springframework.web.servlet.view.velocity.VelocityLayoutView"/>
  10. </bean>

按照上述配置即可在springboot2.x项目中使用velocity模板,欢迎有需要的小伙伴试用,使用过程中有问题可以直接反馈给我、

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号