在appfuse构建的项目中集成velocity的步骤和碰到的问题

使用Velocity无非也就是为了能够真正的实现mvc分层,使得各个团队成员(美工,程序员)可以各尽所长。

在appfuse构建的项目中集成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)定义对应velocity的servlet-mapping:

 

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

(3)将velocity纳入到编码过滤的filter(一般都已经定义经典SetCharacterEncoding):

     < filter-mapping >
        
< filter-name > SetCharacterEncoding </ filter-name >
        
< url-pattern > *.vm </ url-pattern >
    
</ filter-mapping >

2:在项目的web/WEB-INF文件夹中创建并编辑文件toolbox.xml,通常的内容如下:

<? 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文件夹中创建并编辑文件velocity.properties,通常的内容:

input.encoding  =  UTF- 8
#out.encoding 
=  UTF- 8
default.contentType
= text/html ;  charset=UTF-8

---以上三步其实就是普通java web项目集成velocity的必须要做的工作了。
---下面是使用appfuse中的appgen生成velocity代码的要做的工作,这里只做了从table出发的生成过程。

4:在项目中extras/appgen/src中创建模板,这里假设创建的两个文件是List_vm.xdt和Form_vm.xdt
模板的具体内容就要结合xdoclet,velocity和html来编写,不是一个简单的工作!

5:编辑extras/appgen下的build.xml文件,使得在使用ant install-detailed的时候能生成数据表对应的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层可以使用替换的方式把原文件的这部分内容处理掉):

             <!--  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任务,原文件中有以下代码:

<!--  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层可以使用替换的方式把原文件的这部分内容处理掉):

<!--  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任务,原文件中有如下代码:

 

         <!--  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层可以使用替换的方式把原文件的这部分内容处理掉):
         <!--  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命令时就会把生成的文件复制到项目的web/vms文件夹下了。

7:修改项目的根目录下的build.xml:

(1)修改名为copy-web-files的target,使得运行ant deploy时可以将vm文件复制到部署项目的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 >

可以在这个任务后面添加一个任务:

 

             < fileset  dir ="${basedir}/web" >
                
< include  name ="vms/**/*.vm" />
            
</ fileset >

另外,如果不再使用jsp做为view层可以把匹配jsp的fileset节点去掉,这样就不会复制多余的文件到部署的项目中了。

(2)同名的target 中修改另外一个copy任务(顺数第二个),源代码:

 

         < 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中添加一个节点:

 

< exclude  name ="vms/**" />

这样就不会把vms文件夹下的文件当成是普通文件那样复制了

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

ps:基本上就是这么多的步骤,遗漏的地方,欢迎补充!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值