spring5.0适配velocity

公司老项目jar包升级,spring需升级到5.0,原来模板引擎为velocity,不好重新换模板引擎。
百度加上自身试验适配velocity,
不想去官网找的可移步最后去我的github直接拿适配好的,加上依赖,包目录顶级就行。
原理,jvm优先加载class里面相同包名的class(相当于覆盖jar包的)
首先是pom

<dependency>
				<groupId>org.apache.velocity</groupId>
				<artifactId>velocity-tools</artifactId>
				<version>2.0</version>
				<exclusions>
		<exclusion>
			<artifactId>struts-core</artifactId>
			<groupId>org.apache.struts</groupId>
		</exclusion>
		<exclusion>
			<artifactId>struts-taglib</artifactId>
			<groupId>org.apache.struts</groupId>
			</exclusion>
				<exclusion>
					<artifactId>struts-tiles</artifactId>
					<groupId>org.apache.struts</groupId>
				</exclusion>
			</exclusions> 
</dependency>
<dependency>
			<groupId>org.apache.velocity</groupId>
			<artifactId>velocity</artifactId>
			<version>1.7</version>
		</dependency>
		<dependency>
			<groupId>velocity-tools</groupId>
			<artifactId>velocity-tools-view</artifactId>
			<version>1.4</version>
</dependency>

这些需要的jar,可根据需要自己更换版本

去spring 的github地址
https://github.com/spring-projects/spring-framework/blob/4.3.x/spring-context-support/src/main/java/org/springframework/ui/velocity/

https://github.com/spring-projects/spring-framework/blob/4.3.x/spring-webmvc/src/main/java/org/springframework/web/servlet/view/velocity/
下载目录下的所有文件,因为spring4.3是支持velocity的
包目录结构
在自己目录下任意模块新建通路径包并放下去,有错误提示自己改一下

<dependency>
	    <groupId>org.apache.commons</groupId>
	    <artifactId>commons-digester3</artifactId>
	    <version>3.2</version>
	</dependency>

其中可能会提示找不到digester,上面是digester3的jar包,导入后在import处将digester改成digester3
然后把digester.改成getDigester().;
然后去github地址
https://github.com/apache/velocity-tools/tree/1.4
下载其源码包
同上建立路径并导入这些源码
目录结构
一般到此基本配置完成了。
有的会报错nosuchmethod ValueParser.getValue();
我从velocity-tool2.0源码中抠出来getValue()粘贴进ValueParser去就行了。

  public Object getValue(String key)
    {
        return getSource().get(key);
    }

最后放上我自己的适配的文件github地址:https://github.com/zzyzzyzzy123/spring5-velocity

猜你喜欢

转载自blog.csdn.net/qq_27275851/article/details/106833941