根據(jù)網(wǎng)上資料整理
發(fā)布于:2016-10-31
Apache Velocity是一款基于Java的模板引擎,允許使用模版語言來引用Java對象铺厨,與jsp類似。
直接介紹Spring MVC 4 與Velocity整合取试。
一这揣、添加Maven依賴
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
二悔常、增加Spring配置
在Spring mvc的配置文件中增加視圖解析器與velocity配置
<bean id="velocityViewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="suffix" value=".vm" />
<property name="contentType" value="text/html;charset=utf-8" />
<!-- 允許velocity從session中取值 -->
<property name="exposeSessionAttributes" value="true"/>
<!-- 允許重寫session中的值 -->
<property name="allowSessionOverride" value="true"/>
<!-- 如果有多個視圖解析器,該屬性可以指定優(yōu)先級给赞,值越大優(yōu)先級越高 -->
<property name="order" value="9" />
</bean>
<bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/templates/" /> <!-- 模版文件路徑 -->
<property name="velocityProperties">
<props>
<prop key="input.encoding">utf-8</prop>
<prop key="output.encoding">utf-8</prop>
<prop key="directive.set.null.allowed">true</prop>
<prop key="file.resource.loader.cache">true</prop> <!-- 允許緩存 -->
<prop key="velocimacro.context.localscope">true</prop>
<prop key="resource.manager.defaultcache.size">0</prop> <!-- 緩存大小不限制 -->
<!-- 開啟緩存后會定期檢查模版是否有修改机打,單位秒,如不需要檢查可改為0 -->
<prop key="file.resource.loader.modificationCheckInterval">0</prop>
</props>
</property>
</bean>
完成上面兩步后就可以創(chuàng)建模版文件進行開發(fā)了片迅。
Spring mvc 返回模版視圖示例:
@RequestMapping(value = "/test")
public String test(Model model,HttpSession httpSession) {
model.addAttribute("name","測試");
// 在頁面上用$userName取值
httpSession.setAttribute("userName", "test");
// 模版文件路徑templates/other/test.vm
return "other/test";
}
具體的模版語言語法參考:
http://velocity.apache.org/engine/1.7/user-guide.html
三残邀、自定義工具類
velocity雖然內置一些函數(shù),但有時還是難以滿足我們的需求或者比較繁瑣。下面介紹如何創(chuàng)建自定義工具類并在頁面使用它們罐旗。
1膳汪、編寫自定義工具類(以判斷字符串是否為空或者null舉例)
import com.google.common.base.Strings;
public class VelocityStringUtils {
/**
* 判斷字符串是否是null或者空字符串
*
*/
public boolean isNullOrEmpty(String s) {
return Strings.isNullOrEmpty(s);
}
}
2、創(chuàng)建自定義工具類配置文件
<?xml version="1.0" encoding="UTF-8"?>
<toolbox>
<tool>
<!-- 模板中調用名 -->
<key>StringUtils</key>
<scope>application</scope>
<!-- 類全名 -->
<class>com.xxx.utils.VelocityStringUtils</class>
</tool>
</toolbox>
3九秀、修改Spring配置文件
<bean id="velocityViewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="suffix" value=".vm" />
<property name="contentType" value="text/html;charset=utf-8" />
<property name="exposeSessionAttributes" value="true"/>
<property name="allowSessionOverride" value="true"/>
<property name="order" value="9" />
<!-- 指定自定義工具類配置文件位置 -->
<property name="toolboxConfigLocation" value="/WEB-INF/Toolbox.xml" />
</bean>
4、使用自定義工具類
當有用戶的昵稱時顯示歡迎信息粘我。
#if(!$StringUtils.isNullOrEmpty($user.name))
<div>歡迎您:$user.name</div>
#end