Velocity是一種Java模板引擎。
和JSP,Freemarker差不多,都是用來展示網頁內容的。
和JSP不同的是velocity只能顯示Action中的數據,不能處理數據。不能寫java代碼,但是可以使用Velocity標記。
Velocity的頁面(模版)可是是任何類型(text/html)的文件。
當Velocity應用於web開發時,Velocity將java代碼從web頁面中分離出來,界面設計人員可以和java程序開發人員同步開發一個遵循MVC架構的web站點,
也就是說,頁面設計人員可以只關注頁面的顯示效果,而由java程序開發人員關注業務邏輯編碼。
下面簡要描述一下在spring mvc中配置velocity的步驟:
1、引入velocity所需要的包
2、添加配置信息
3、測試
步驟詳解:
1、引入velocity包:velocity-1.7.jar、velocity-tools-2.0.jar、spring-context-support-4.2.5.RELEASE.jar(無視版本號)
pom文件配置的話,引入下面jar包即可
<!--velocity start-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.6</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.2.5.RELEASE</version>
</dependency>
<!--velocity end-->
2、在配置文件裏添加配置信息
VelocityConfigurer負責在spring中設置Velocity引擎。這裏,通過屬性resourceLoaderPath告訴Velocity到哪裏尋找它的模板。建議將模板放到WEB-INF下的某個子目錄下,可以保證這些模板不能被直接訪問。
在配置屬性時,有兩種配置方法:
(1)通過配置文件方式
<!--模板信息配置-->
<bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/velocity/" /><!-- 模板存放的路徑 -->
<property name="configLocation" value="classpath:velocity.properties"/>
</bean>
velocity.properties:
input.encoding=UTF-8
output.encoding=UTF-8
directive.foreach.counter.name=loopCounter
directive.foreach.counter.initial.value=0
(2)通過屬性的方式:
<bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="WEB-INF/velocity/" /><!-- 設置模板防止位置-->
<property name="velocityProperties">
<props>
<prop key="directive.foreach.counter.name">loopCounter</prop>
<prop key="directive.foreach.counter.initial.value">0</prop>
<prop key="input.encoding">UTF-8</prop><!-- 指定模板引擎進行模板處理的編碼 -->
<prop key="output.encoding">UTF-8</prop><!-- 指定輸出流的編碼 -->
</props>
</property>
</bean>
我第一次配置時,用的第一種方式,出現一個問題,就是走完後臺後,頁面不進行跳轉,一直是layout.vm。
後來改爲第二種方式,問題解決,再改爲第一種配置,問題再現不了。如果有朋友碰到類似問題,可以試試另一種方式配置。
3、配置velocity解析視圖
<!-- 配置視圖的顯示 -->
<bean id="ViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="/" /><!-- 視圖文件的前綴,即存放的路徑 -->
<property name="suffix" value=".vm" /><!-- 視圖文件的後綴名 -->
<!--<property name="toolboxConfigLocation" value="/WEB-INF/tools.xml" /><!–toolbox配置文件路徑–>-->
<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屬性引用名稱-->
</bean>
VelocityViewResolver和Velocity的關係與InternalResourceViewResolver和JSP的關係相似。
InternalResourceViewResolver使用prefix屬性和suffix屬性由視圖的邏輯名構造出模板文件路徑,這樣在Controller中的ModelAndView中直接通過文件名找模板。
4、測試頁面
Action方法:
@RequestMapping(value="hello")
public ModelAndView printWelcome(HttpServletRequest request,HttpServletResponse response) {
ModelAndView mav= new ModelAndView();
mav.addObject("city","test");
mav.setViewName("hello");
return mav;
}
5、在/WEB-INF/velocity/ 路徑下信件hello.vm
<html>
<body>
<h1>${city}</h1>
</body>
</html>
6、啓動項目,訪問http://localhost:8080/hello.action