最近正在做的項目前端使用了Velocity進行View層的數據渲染,之前沒有接觸過,草草過了一遍,就上手開始寫,現在又回頭細致的看了一遍,做個筆記.
velocity是一種基於java的模板引擎技術,有點類似與JSP,它允許頁面設計者引用Java中定義的方法。前端頁面設計者和后端Java開發者能夠同時使用MVC的模式開發網站,這樣前端能夠把精力放在頁面的設計上,后端也可以把精力放在代碼開發上。Velocity把Java代碼從Web頁面中分離, 使網站可維護性更強.
注:項目使用的是Spring+Springmvc+mybatsi+maven.
項目中使用velocity
1.添加velocity依賴的jar包,這里使用maven管理依賴,所以只需要在pom.xml中添加:
<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>
2.因為項目使用到springmvc,所以項目中的web.xml需要配置一下DispatcherServlet,如下:
<!--配置springmvc的前端控制器 --> <servlet> <servlet-name>Spring web</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/web-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Spring web</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
3.注意這里的contextConfigLocation參數,它指向了classpath路徑下的spring目錄下的web-context.xml文件,這里就是我們的spring的配置文件,我們將在這里配置Velocity的bean.
<!-- 配置VelocityConfigurer,負責在spring中設置Velocity引擎。通過屬性resourceLoaderPath告訴Velocity到哪里尋找它的模板。 通常將模板放到WEB-INF下的某個子目錄下,可以保證這些模板不能被直接訪問。--> <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath" value="/WEB-INF/velocity/templates"/> <property name="configLocation" value="classpath:velocity/velocity.properties"/> </bean> <!--配置試圖解析器--> <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> <property name="cache" value="false"/> <property name="prefix" value=""/> <property name="suffix" value=".vm"/> <property name="contentType" value="text/html;charset=utf-8"/> <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/> <property name="exposeSpringMacroHelpers" value="true"/> <property name="exposeRequestAttributes" value="true"/> <property name="exposeSessionAttributes" value="true"/> <property name="allowSessionOverride" value="true"/> <property name="allowRequestOverride" value="true"/> </bean>
在類路徑的velocity目錄中添加velocity.properties文件,在這個配置文件中可以自定義一些velocity默認配置:
input.encoding=UTF-8
output.encoding=UTF-8
這里我只是簡單定義了輸入輸出的編碼,對於不同的項目需要可以定義不同的參數,這一篇博文,對velocity.properties內容有比較詳細的解釋,感興趣可以移步:Velocity配置詳解
在web-context.xml文件中,還可以看到<property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/>,在web-inf路徑下,建一個toolbox.xml文件,如下:
<?xml version="1.0" encoding="UTF-8" ?> <toolbox> <tool> <key>UrlUtil</key> <class>com.nali.common.util.UrlUtil</class> <scope>application</scope> </tool> <tool> <key>VersionUtil</key> <class>com.ximalaya.shop.api.domain.util.VersionUtil</class> <scope>application</scope> </tool> </toolbox>
這個配置文件可以讓我們在vm中使用后台java類中定義的方法.
以上就完成了一個完整的ssm項目中使用velocity的流程,我們可以在"/WEB-INF/velocity/templates"下新建一些.vm的文件用於展示數據,對於velocity的語法使用,可以參考這篇網友翻譯的官方文檔:http://ifeve.com/apache-velocity-dev/
