SpringMVC整合Velocity模版引擎

nbpd7711 8年前發布 | 18K 次閱讀 Spring MVC Web框架

來自: http://blog.csdn.net//chenleixing/article/details/45015929


Velocity是一個基于java的模板引擎(template engine),它允許任何人僅僅簡單的使用模板語言(template language)來引用由java代碼定義的對象。

配置:

1.在pom.xml增加依賴的velocity包

[html]   view plain copy print ?
  1. <dependency>  
  2.     <groupId>velocity</groupId>  
  3.     <artifactId>velocity</artifactId>  
  4.     <version>1.5</version>  
  5. </dependency>  

2.在servlet-context.xml中增加以下內容,如果有jsp的配置先注釋掉

[html]   view plain copy print ?
  1. <beans:bean id="velocityConfig"  
  2. <span style="white-space:pre">  </span>class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">  
  3.     <beans:property name="resourceLoaderPath" value="/WEB-INF/views" />  
  4.     <beans:property name="configLocation" value="classpath:common/velocity.properties" />  
  5. </beans:bean>  
  6.   
  7. <beans:bean id="velocityViewResolver"  
  8.     class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">  
  9.     <beans:property name="suffix" value=".htm" />  
  10. </beans:bean>  


3.在resources/common目錄下創建velocity.properties

[html]   view plain copy print ?
  1. #encoding  
  2. input.encoding  =UTF-8  
  3. output.encoding=UTF-8  
  4. contentType=text/html;charset=UTF-8  
  5.   
  6. #autoreload when vm changed  
  7. file.resource.loader.cache=false  
  8. file.resource.loader.modificationCheckInterval  =1  
  9. velocimacro.library.autoreload=false  

4.新建testController

[java]   view plain copy print ?
  1. @RequestMapping(value="/test")  
  2. @Controller  
  3. public class TestController {  
  4.     @RequestMapping(value="/index")  
  5.     public String index(Model model) {  
  6.         String name = "tester";  
  7.         model.addAttribute("name", name);  
  8.         return "test/index";  
  9.     }  
  10. }  

5.新建test/index.htm模板

<html>
<head>
</head>
<body>
hello $name!
</body>
</html>


6.訪問http://localhost/test/index

顯示 hello tester!


---------------------------------------------------------------------------------------------------------------------------------------------------------------

Velocity的基本語法:

1、"#"用來標識Velocity的腳本語句,包括#set、#if 、#else、#end、#foreach、#end、#iinclude、#parse、#macro等;
如:
#if($info.imgs)
<img src="$info.imgs" border=0>
#else
<img src="noPhoto.jpg">
#end


2、"$"用來標識一個對象(或理解為變量);如
如:$i、$msg、$TagUtil.options(...)等。


3、"{}"用來明確標識Velocity變量;
比如在頁面中,頁面中有一個$someonename,此時,Velocity將把someonename作為變量名,若我們程序是想在someone這 個變量的后面緊接著顯示name字符,則上面的標簽應該改成${someone}name。


4、"!"用來強制把不存在的變量顯示為空白。
如當頁面中包含$msg,如果msg對象有值,將顯示msg的值,如果不存在msg對象同,則在頁面中將顯示$msg字符。這是我們不希望的,為了把不存 在的變量或變量值為null的對象顯示為空白,則只需要在變量名前加一個“!”號即可。
如:$!msg


5、循#foreach( $info in $list) $info.someList #end,環讀取集合list中的對象

#foreach( $info in $hotL包含文件#inclue("模板文件名")或#parse("模板文件名")st1) 
<a href="/blog/list?&cid=$!info.cid" target="_blank">$!info.title</a><br>
#end 
上面的腳本表示循環遍歷hotList1集合中的對象,并輸出對象的相關內容。


6、包含文件#inclue("模板文件名")或#parse("模板文件名")

主要用于處理具有相同內容的頁面,比如每個網站的頂部或尾部內容。
使用方法,可以參考EasyJF開源Blog及EasyJF開源論壇中的應用!
如:#parse("/blog/top.html")或#include("/blog/top.html")
parse與include的區別在于,若包含的文件中有Velocity腳本標簽,將會進一步解析,而include將原樣顯示。

轉自:http://blog.csdn.net/wuxinzaiyu/article/details/8363627

 本文由用戶 nbpd7711 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!