SpringBoot1.5x以上版不支持velocity的解决方案
推荐一套教程
Go语言开发分布式任务调度 轻松搞定高性能Crontab
Q群:702101215
下载地址:版权保护,不再提供网络下载地址
正文开始:
springboot 在1.4版本中 融合了velocity,freemarker和thymeleaf模板。这个版本,如果想使用这些模板,只需要引入相应的starter pom就可以了。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
2.使用xml文件 自定义 velocity解析器! velocityConfig.xml具体信息
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="mvcVelocityEngine"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/templates/" /><!-- 模板存放的路径 --> <!--原文有个加载配置文件的配置,我自己测下来没用 -->
<!-- <property name="configLocation" value="classpath:velocity.properties"/> --><!--建议使用这种 -->
<property name="velocityProperties"><!--这个不加,加载出来的页面是乱码 -->
<props>
<prop key="input.encoding">utf-8</prop>
<prop key="output.encoding">utf-8</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="false" />
<property name="prefix" value="/templates/" /><!-- 视图文件的前缀,即存放的路径 -->
<property name="suffix" value=".vm" /><!-- 视图文件的后缀名 -->
<property name="dateToolAttribute" value="date" /><!--日期函数名称 -->
<property name="numberToolAttribute" value="number" /><!--数字函数名称 -->
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="exposeSpringMacroHelpers" value="true" /><!--是否使用spring对宏定义的支持 -->
<property name="exposeRequestAttributes" value="true" /><!--是否开放request属性 -->
<property name="requestContextAttribute" value="rc" /><!--request属性引用名称 -->
<!--<property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/> --><!--这个没用到,vm页面内自定义的函数也可以使用 -->
</bean>
</beans>
3.将xml 引入到项目中
package com.ppl.velocity;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@ImportResource({"classpath:velocityConfig.xml"})
public class VelocityApplication {
public static void main(String[] args) {
SpringApplication.run(VelocityApplication.class, args);
}
}
4、如果想使用velocity 的自定义toolboox,还需要做如下的配置
在main下构建webapp目录,webapp目录下创建WEB-INF文件夹,然后把toolbox配置文件放到里边。
扫描二维码关注公众号,回复:
4029382 查看本文章
把toolbox 的xml地址 放到之前的 velocityConfig.xml里。
经过我多次测试,只有放在这里,这有这样做才能被 velacity的 manager读取。所以只能是这样!
看清楚我下面的toolbox.xml怎么写的了么! xml的 root 元素 用<toolbox>替换<tools>!
<?xml version="1.0" encoding="UTF-8"?>
<toolbox>
<data key="foo">this is foo</data>
<tool>
<key>urlSign</key>
<scope>request</scope>
<class>com.github.qinyinglian.common.UrlSignUtil</class>
</tool>
</toolbox>
推荐一个教程:
Go语言开发分布式任务调度 轻松搞定高性能Crontab
Q群:702101215
下载地址:版权保护,不再提供网络下载地址