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

阅读: 评论:0

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

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

解决SpringBoot1.5.x版本对Velocity模板不支持的方案
项目构建工具Gradle adle配置文件 SpringBoot版本:1.5.9.RELEASE
引入SpringBoot集成Velocity模板的依赖
进入项目adle配置文件所在目录
使用gradle build --refresh-dependencies命令刷新依赖
报错的原因很明显,org.springframework.boot:spring-boot-starter-velocity:1.5.9.RELEASE这个依赖不能加载进来,原因是找不到这个依赖。于是上网查找原因说是SpringBoot1.5.x以后的版本不再支持Velocity这个模板,抱着疑问去Spring官网上寻找答案,在GitHub上关注的SpringBoot项目找到了答案,进入到自己关注的开源项目中查看,
现在SpringBoot版本已经是2.0.x了,我们在Wiki中查找历史版本
来看1.4版本和1.5版本有何区别
先看1.4版本,Spring4.3之后将Velocity标记为过时,但是此时SpringBoot1.4版本仍然是支持Velocity模板的
再看1.5版本,此时已经移除对Velocity模板的依赖,不再支持
由于此时项目使用的版本已经是1.5.9.RELEASE,为了项目稳定的运行,不能降低SpringBoot版本,于是经过讨论有两个解决方案 方案一:使用带版本号的SpringBoot整合Velocity版本 SpringBoot版本保持不变,使用spring-boot-starter-velocity的低版本依赖,最终选择的版本是1.4.6.RELEASE,重新替换SpringBoot对Velocity的依赖
重新刷新依赖
从上面可以看出其它的依赖仍然是1.5.9版本,而SpringBoot集成Velocity版本是1.4.6,我们可以来看看这个jar包到底有什么
pom.properties配置文件定义jar版本信息
l配置文件配置引入的其它依赖,下面是配置文件内容 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=".0.0" xmlns:xsi="" xsi:schemaLocation=".0.0 .0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starters</artifactId> <version>1.4.6.RELEASE</version> </parent> <artifactId>spring-boot-starter-velocity</artifactId> <name>Spring Boot Velocity Starter</name> <description>Starter for building MVC web applications using Velocity views. Deprecated since 1.4</description> <url>/</url> <organization> <name>Pivotal Software, Inc.</name> <url>;/url> </organization> <properties> <main.basedir>${basedir}/../..</main.basedir> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> </dependency> <dependency> <groupId>commons-digester</groupId> <artifactId>commons-digester</artifactId> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-tools</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> </dependencies> </project> 从上面依赖中看出我们关心的重点是两个jar包 velocity和velocity-tools
接下来就是重新配置Velocity 上面仅仅是将依赖导入了项目, SpringBoot1.5.x版本并不会主动加载注入Velocty模板相关类,因此需要我们在使用启动的时候主动创建VelocityEngine,注入到Spring容器中,在初始化VelocityEngine这个Bean的同时需要去加载默认两个配置文件velocity.properties配置文件和directive.properties配置文件,因为加载的机制有了变化,那么我们也要响应的修改默认配置文件的一些属性配置项, 因此在classpath下新建velocity.properties配置文件
新建的 velocity.properties 配置文件内容 resource.loader=jar source.loader.class=org.apache.source.loader.ClasspathResourceLoader source.loader.cache=true ding=UTF-8 而Velocity默认配置文件内容中加载是这样的 加载部分
和编码部分
是以file文件的形式加载,我们需要修改加载机制,就像我们配置文件中配置的那样使用jar的形式去加载,唯一额外的一项就是设置编码格式为UTF-8。 我们在启动项目的时候需要加载这个Bean,于是在SpringBoot启动类中加入如下代码:
这样我们就可以在SpringBoot1.5.9版本中使用Velocity模板了 上述做法是从stackoverflow参考的,地址为:
国外的大神解决方案总是那么平易近人,让人容易理解。 解决方案二:项目中直接引入Velocity模板,使用配置的方式Spring去整合Velocity adle配置引入Velocity依赖
刷新依赖
之后就是使用Spring整合Velocity模板,也就是将VelocityEngine这个Bean注入到Spring容器中,整合的方式很多,虽然项目是SpringBoot,但是依然可以使用Spring配置文件方式整合, 在整合之前还需要引入支持Velocity的一个重要jar包,这个jar包很重要,必须引入,否则无法注册VelocityEngine这个Bean。
刷新依赖
接下来是l配置文件需要引入Spring整合Velocity的配置
l配置文件内容 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="" xmlns:xsi="" xmlns:context="" xsi:schemaLocation=" .0.xsd .0.xsd" default-autowire="byName"> <!-- velocity模板引擎 --> <bean id=" velocityEngine " class=" org.springframework.ui.velocity.VelocityEngineFactoryBean "> <property name=" velocityProperties "> <props> <prop key="resource.loader">class</prop> <prop key=&#source.loader.class">org.apache.source.loader.ClasspathResourceLoader</prop> <prop key="velocimacro.library" /> <prop key="unter.name">loopCounter</prop> <prop key="unter.initial.value">1</prop> <prop key=&#source.loader.cache">true</prop> <prop key="parser.pool.size">50</prop> <prop key=&#ding">UTF-8</prop> <prop key=&#ding">UTF-8</prop> </props> </property> </bean> </beans> 从上面整合的信息来看最重要的就是使用spring-context-support这个jar包下的 org.springframework.ui.velocity.VelocityEngineFactoryBean类来创建名称为velocityEngine的这个Bean,注入到Spring容器中。具体加载信息可以看这个类的源码,这里不再赘述。 对velocityProperties的配置是来自VelocityEngineFactoryBean的父类VelocityEngineFactory中的一个Map类型的属性velocityProperties,这里存储的是加载默认配置文件的内容,上面也提到过,源码中已经体现的很明白。 然后在SpringBoot启动类家需要加载上面配置的l配置文件 只要在启动类上加上 @ImportResource({"classpath:/l"})即可
到这里就可以使用Velocity模板了。 参考文章: 以上就是两种在SpringBoot1.5.x版本中使用Velocity模板,虽然破坏了SpringBoot的灵活性,但是也不失一种解决升级SpringBoot版本的方案。



本文发布于:2024-01-27 23:31:45,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17063695073304.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:不支持   模板   版本   方案   Velocity
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23