SpringBoot1.5x以上版不支援velocity的解決方案
阿新 • • 發佈:2018-11-11
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配置檔案放到裡邊。
把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
下載地址:版權保護,不再提供網路下載地址