Google Code Prettify

2009年5月18日 星期一

Spring MVC + Velocity

參考手冊

http://www.javaworld.com.tw/confluence/display/opensrc/Spring
http://velocity.apache.org/engine/devel/user-guide.html


開發外掛

SpringIDE http://springide.org/updatesite/
VeloEclipse http://veloeclipse.googlecode.com/svn/trunk/update/
Dreamweaver Velocity http://velocity.gilluminate.com/


設定
  1. 相關jar檔
    commons-collections.jar
    commons-lang.jar
    commons-logging.jar
    log4j-1.2.15.jar
    spring.jar
    spring-web.jar
    spring-webmvc.jar
    velocity-1.6.2.jar
    velocity-tools-generic-1.4.jar
    velocity-tools-view-1.4.jar

  2. web.xml
    將所有網址.do的頁面導向spring servlet做處理。

  3. spring-servlet.xml
    Bean id=velocityConfig,設定Velocity參數。
    Bean id=viewResolver,解析*.vm頁面。



    Bean id=paramMethodResolver,解析url上的method參數,導入至對應的Method,預設值為view。
    Bean id=thinkonDao,宣告為Bean方式,方便其它Bean注入引用。
    Bean id=urlMapping,解析url,導入至對應的Bean id。
    Bean id=indexAction,測試範例,對應/index.do,注入dao引用。




範例
  1. index.java (Spring MultiActionController)
    輸出參數至ModelAndView,導入index.vm做剖析輸出畫面。

  2. index.vm (Velocity)
    ${title}會對應至ModelAndView的title參數。
    #set($a = ‘abcsdfsd’) 為宣告頁面變數。
    ${a}會對應至#set($a)的值。


  3. 輸出結果
    ${uic}因尚未找到任何對應的變數,所以不處理。

沒有留言: