Java Blog for Alex Wan

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

          統(tǒng)計

          留言簿(10)

          BlogJava

          Blogs

          DIV+CSS

          JQuery相關(guān)

          友情鏈接

          常去的地方

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

          閱讀排行榜

          評論排行榜

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

          使用Velocity無非也就是為了能夠真正的實現(xiàn)mvc分層,使得各個團隊成員(美工,程序員)可以各盡所長。

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

          1:修改web.xml使得項目支持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)定義對應(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:在項目的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:在項目的build/web/classes文件夾中創(chuàng)建并編輯文件velocity.properties,通常的內(nèi)容:
          input.encoding = UTF-8
          #out.encoding = UTF-8
          default.contentType=text/html; charset=UTF-8

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

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

          5:編輯extras/appgen下的build.xml文件,使得在使用ant install-detailed的時候能生成數(shù)據(jù)表對應(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"/>
          我們要在這個后面添加以下代碼(如果不使用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指定生成的臨時文件。

          (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"/>

          我們要在這個后面添加以下代碼(如果不使用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"/>

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

          (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>

          我們要在這個后面添加以下代碼(如果不使用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命令時就會把生成的文件復(fù)制到項目的web/vms文件夾下了。

          7:修改項目的根目錄下的build.xml:

          (1)修改名為copy-web-files的target,使得運行ant deploy時可以將vm文件復(fù)制到部署項目的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>

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

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

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

           <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中添加一個節(jié)點:
          <exclude name="vms/**"/>

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

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

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



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

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


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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 昌黎县| 漳州市| 洪湖市| 峨眉山市| 治县。| 凤台县| 南溪县| 区。| 南宫市| 准格尔旗| 铅山县| 泾阳县| 聂拉木县| 赤城县| 永康市| 惠安县| 靖江市| 定南县| 玉田县| 三都| 阳东县| 商丘市| 手游| 新野县| 南汇区| 广西| 东平县| 峨边| 五华县| 马公市| 嵊州市| 老河口市| 屏东市| 石家庄市| 武穴市| 梧州市| 西林县| 美姑县| 英德市| 台山市| 阳春市|