Java Blog for Alex Wan

          Let life be beautiful like summer flowers and death like autumn leaves.

          統(tǒng)計(jì)

          留言簿(10)

          BlogJava

          Blogs

          DIV+CSS

          JQuery相關(guān)

          友情鏈接

          常去的地方

          數(shù)據(jù)供應(yīng)

          閱讀排行榜

          評(píng)論排行榜

          在appfuse構(gòu)建的項(xiàng)目中集成velocity的步驟和碰到的問題

          使用Velocity無非也就是為了能夠真正的實(shí)現(xiàn)mvc分層,使得各個(gè)團(tuán)隊(duì)成員(美工,程序員)可以各盡所長(zhǎng)。

          在appfuse構(gòu)建的項(xiàng)目中集成velocity的步驟和碰到的問題 :

          1:修改web.xml使得項(xiàng)目支持velocity

          (1)定義名為velocity的servlet:

          <servlet>
                  
          <servlet-name>velocity</servlet-name>
                  
          <servlet-class>
                      org.apache.velocity.tools.view.servlet.VelocityViewServlet
                  
          </servlet-class>
                  
          <init-param>
                      
          <param-name>org.apache.velocity.toolbox</param-name>
                      
          <param-value>/WEB-INF/toolbox.xml</param-value>
                  
          </init-param>
                  
          <init-param>
                      
          <param-name>org.apache.velocity.properties</param-name>
                      
          <param-value>
                          /WEB-INF/classes/velocity.properties
                      
          </param-value>
                  
          </init-param>
                  
          <load-on-startup>10</load-on-startup>
              
          </servlet>

          (2)定義對(duì)應(yīng)velocity的servlet-mapping:

            <servlet-mapping>
                  
          <servlet-name>velocity</servlet-name>
                  
          <url-pattern>*.vm</url-pattern>
              
          </servlet-mapping>

          (3)將velocity納入到編碼過濾的filter(一般都已經(jīng)定義經(jīng)典SetCharacterEncoding):

           <filter-mapping>
                  
          <filter-name>SetCharacterEncoding</filter-name>
                  
          <url-pattern>*.vm</url-pattern>
              
          </filter-mapping>
          2:在項(xiàng)目的web/WEB-INF文件夾中創(chuàng)建并編輯文件toolbox.xml,通常的內(nèi)容如下:
          <?xml version="1.0" encoding="UTF-8"?>
          <toolbox>
            
          <tool>
               
          <key>link</key>
               
          <scope>request</scope>
               
          <class>org.apache.velocity.tools.struts.StrutsLinkTool</class>
            
          </tool>
            
          <tool>
               
          <key>text</key>
               
          <scope>request</scope>
               
          <class>org.apache.velocity.tools.struts.MessageTool</class>
            
          </tool>
            
          <tool>
               
          <key>errors</key>
               
          <scope>request</scope>
               
          <class>org.apache.velocity.tools.struts.ErrorsTool</class>
            
          </tool>
            
          <tool>
               
          <key>form</key>
               
          <scope>request</scope>
               
          <class>org.apache.velocity.tools.struts.FormTool</class>
            
          </tool>
            
          <tool>
               
          <key>tiles</key>
               
          <scope>request</scope>
               
          <class>org.apache.velocity.tools.struts.TilesTool</class>
            
          </tool>
            
          <tool>
               
          <key>validator</key>
               
          <scope>request</scope>
               
          <class>org.apache.velocity.tools.struts.ValidatorTool</class>
            
          </tool>
          </toolbox>

          3:在項(xiàng)目的build/web/classes文件夾中創(chuàng)建并編輯文件velocity.properties,通常的內(nèi)容:
          input.encoding = UTF-8
          #out.encoding = UTF-8
          default.contentType=text/html; charset=UTF-8

          ---以上三步其實(shí)就是普通java web項(xiàng)目集成velocity的必須要做的工作了。
          ---下面是使用appfuse中的appgen生成velocity代碼的要做的工作,這里只做了從table出發(fā)的生成過程。

          4:在項(xiàng)目中extras/appgen/src中創(chuàng)建模板,這里假設(shè)創(chuàng)建的兩個(gè)文件是List_vm.xdt和Form_vm.xdt
          模板的具體內(nèi)容就要結(jié)合xdoclet,velocity和html來編寫,不是一個(gè)簡(jiǎn)單的工作!

          5:編輯extras/appgen下的build.xml文件,使得在使用ant install-detailed的時(shí)候能生成數(shù)據(jù)表對(duì)應(yīng)的vm文件.

          (1):在名為gen的target中添加template,原文件有以下的代碼:


          <!-- Form JSP -->
                      
          <template templateFile="${template.dir}/Form_jsp.xdt"
                                acceptAbstractClasses
          ="false"
                                prefixWithPackageStructure
          ="false"
                                destinationFile
          ="${gen.dir}/web/pages/{0}FormTemp.jsp"/>
                      
          <!-- List JSP -->
                      
          <template templateFile="${template.dir}/List_jsp.xdt"
                                acceptAbstractClasses
          ="false"
                                prefixWithPackageStructure
          ="false"
                                destinationFile
          ="${gen.dir}/web/pages/{0}ListTemp.jsp"/>
          我們要在這個(gè)后面添加以下代碼(如果不使用jsp作為view層可以使用替換的方式把原文件的這部分內(nèi)容處理掉):
          <!-- Form VM -->
                      
          <template templateFile="${template.dir}/Form_vm.xdt"
                        acceptAbstractClasses
          ="false"
                        prefixWithPackageStructure
          ="false"
                        destinationFile
          ="${gen.dir}/web/vms/{0}FormTemp.vm"/>
                      
          <!-- List VM -->
                         
          <template templateFile="${template.dir}/List_VM.xdt"
                        acceptAbstractClasses
          ="false"
                        prefixWithPackageStructure
          ="false"
                        destinationFile
          ="${gen.dir}/web/vms/{0}ListTemp.vm"/>

          這里,templateFile里指定模板文件,destinationFile指定生成的臨時(shí)文件。

          (2):在名字同樣為gen的target中添加move任務(wù),原文件中有以下代碼:


          <!-- Make first character of JSP filenames lowercase -->
                  
          <move file="${build.dir}/${gen.dir}/web/pages/${model.name}ListTemp.jsp"
                      tofile
          ="${build.dir}/${gen.dir}/web/pages/${app.module.slash.after}${model.name.lowercase}List.jsp"/>
                  
          <move file="${build.dir}/${gen.dir}/web/pages/${model.name}FormTemp.jsp"
                      tofile
          ="${build.dir}/${gen.dir}/web/pages/${app.module.slash.after}${model.name.lowercase}Form.jsp"/>

          我們要在這個(gè)后面添加以下代碼(如果不使用jsp作為view層可以使用替換的方式把原文件的這部分內(nèi)容處理掉):
          <!-- Make first character of Velocity filenames lowercase -->
                  
          <move file="${build.dir}/${gen.dir}/web/vms/${model.name}ListTemp.vm"
                      tofile
          ="${build.dir}/${gen.dir}/web/vms/${app.module.slash.after}${model.name.lowercase}List.vm"/>
                  
          <move file="${build.dir}/${gen.dir}/web/vms/${model.name}FormTemp.vm"
                      tofile
          ="${build.dir}/${gen.dir}/web/vms/${app.module.slash.after}${model.name.lowercase}Form.vm"/>

          這樣生成的臨時(shí)文件就會(huì)被重命名(有點(diǎn)懷疑這樣做的必要性,暫且先這樣做吧)。

          (3):在名為merge-common的target中添加copy任務(wù),原文件中有如下代碼


            <!-- copy jsp files -->
                  
          <echo>Copying all web files into main project, overwrite="${overwrite}"</echo>
                  
          <copy todir="../../web/pages">
                      
          <fileset dir="${generated.dir}/web/pages" includes="**/${model.name.lowercase}*.jsp"/>
                  
          </copy>

          我們要在這個(gè)后面添加以下代碼(如果不使用jsp作為view層可以使用替換的方式把原文件的這部分內(nèi)容處理掉):
           <!-- copy velocity files -->
                  
          <echo>Copying all velocity files into main project, overwrite="${overwrite}"</echo>
                  
          <copy todir="../../web/vms">
                      
          <fileset dir="${generated.dir}/web/vms" includes="**/${model.name.lowercase}*.vm"/>
                  
          </copy>

          這樣在使用ant install-detailed命令時(shí)就會(huì)把生成的文件復(fù)制到項(xiàng)目的web/vms文件夾下了。

          7:修改項(xiàng)目的根目錄下的build.xml:

          (1)修改名為copy-web-files的target,使得運(yùn)行ant deploy時(shí)可以將vm文件復(fù)制到部署項(xiàng)目的WEB-INFO文件夾下(放在WEB-INF下是為了防止直接訪問 )。
          參考的源代碼:


           <!-- Copy JSP Pages under WEB-INF/pages -->
                  
          <copy todir="${webapp.target}/WEB-INF">
                      
          <fileset dir="${basedir}/web">
                          
          <include name="pages/**/*.jsp"/>
                      
          </fileset>
                      
          <fileset dir="${struts.dir}" includes="*.xml"/>
                      
          <fileset dir="${basedir}/web/WEB-INF" includes="**/*-resources.xml"/>
                      
          <filterset refid="db.variables"/>
                  
          </copy>

          可以在這個(gè)任務(wù)后面添加一個(gè)任務(wù):
          <fileset dir="${basedir}/web">
                          
          <include name="vms/**/*.vm"/>
                      
          </fileset>

          另外,如果不再使用jsp做為view層可以把匹配jsp的fileset節(jié)點(diǎn)去掉,這樣就不會(huì)復(fù)制多余的文件到部署的項(xiàng)目中了。

          (2)同名的target 中修改另外一個(gè)copy任務(wù)(順數(shù)第二個(gè)),源代碼:

           <copy todir="${webapp.target}" includeEmptyDirs="no">
                      
          <fileset dir="${basedir}/web">
                          
          <include name="**"/>
                          
          <exclude name="pages/**"/>
                          
          <exclude name="**/classes/**"/>
                          
          <exclude name="**/*-resources.xml"/>
                      
          </fileset>
                  
          </copy>

          在fileset中添加一個(gè)節(jié)點(diǎn):
          <exclude name="vms/**"/>

          這樣就不會(huì)把vms文件夾下的文件當(dāng)成是普通文件那樣復(fù)制了

          8:在struts-config.xml修改forwards,使得它們指向特定的vm。

          ps:基本上就是這么多的步驟,遺漏的地方,歡迎補(bǔ)充!



          Let life be beautiful like summer flowers and death like autumn leaves.

          posted on 2008-06-07 09:54 Alexwan 閱讀(539) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 隆安县| 郧西县| 张家界市| 偃师市| 民权县| 东明县| 调兵山市| 壤塘县| 聊城市| 阳高县| 延寿县| 尼木县| 武邑县| 固安县| 铁力市| 华阴市| 敖汉旗| 丹东市| 海门市| 酉阳| 潼关县| 石泉县| 临泽县| 驻马店市| 龙南县| 新宾| 泾阳县| 安丘市| 区。| 宁陵县| 盱眙县| 苍溪县| 绥芬河市| 灵武市| 阿拉善盟| 常德市| 姚安县| 沈阳市| 五常市| 高密市| 南丰县|