解决SpringBoot2.x版本对Velocity模板不支持的方案

栏目: Java · 发布时间: 7年前

内容简介:直接编译打包在项目中直接添加如下依赖:并添加如下配置:

背景

在springboot1.4.x版本之前对velocity的模板支持的是相当好的,不止出于什么原因springboot从1.5.x以后停止了对velocity的支持,甚至在2.x版本中移除了和velocity的相关代码。目前手上有些项目使用的是velocity模板引擎,同时也使用了springboot,现在想升级到springboot2.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的相关代码;

步骤2:

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文件拷贝过来,整体代码架构如下图: 解决SpringBoot2.x版本对Velocity模板不支持的方案

直接编译打包

接入使用:

在项目中直接添加如下依赖:

<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中添加视图解析配置:

<!-- 设置视图解析工具 -->  
    <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模板,欢迎有需要的小伙伴试用,使用过程中有问题可以直接反馈给我


以上所述就是小编给大家介绍的《解决SpringBoot2.x版本对Velocity模板不支持的方案》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

生命3.0

生命3.0

[美] 迈克斯·泰格马克 / 汪婕舒 / 浙江教育出版社 / 2018-6 / 99.90元

《生命3.0》一书中,作者迈克斯·泰格马克对人类的终极未来进行了全方位的畅想,从我们能活到的近未来穿行至1万年乃至10 亿年及其以后,从可见的智能潜入不可见的意识,重新定义了“生命”“智能”“目标”“意识”,并澄清了常见的对人工智能的误解,将帮你构建起应对人工智能时代动态的全新思维框架,抓住人类与人工智能共生演化的焦点。 迈克斯·泰格马克不仅以全景视角探讨了近未来人工智能对法律、战争、就业和......一起来看看 《生命3.0》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换