采用velocity根据模块自动生成java类

系统 2273 0

import  java.io.BufferedWriter;
import  java.io.File;
import  java.io.FileWriter;
import  java.io.IOException;
import  java.io.StringWriter;
import  java.util.Properties;
import  org.apache.velocity.Template;
import  org.apache.velocity.VelocityContext;
import  org.apache.velocity.app.VelocityEngine;

public   class  Test  ... {
    
private   static  Properties props  =   null ;    
    
/** */ /**
     * 
@param  args
     * 
@throws  Exception 
     
*/

    
public   static   void  main(String[] args)  throws  Exception  ... {
        
//  TODO Auto-generated method stub
         /**/ /*  first, get and initialize an engine  */
          VelocityEngine ve 
=   new  VelocityEngine();
          ve.init();
          
/**/ /*  next, get the Template  */
          Template t 
=  ve.getTemplate(  " src/Action.vm " , " gb2312 "  );  
        
//           Template t = ve.getTemplate( "src/Action.vm" );
          
// 如果改为上面则出现乱码.
          
          
/**/ /*  create a context and add data  */
          VelocityContext context 
=   new  VelocityContext();
          Object[] classNames
= null ;
//               classNames.add("Jgss");
//               classNames.add("Jgfs");
//               classNames.add("Tcfj");
          classNames = prop2List();
          
for ( int  i = 0 ;i < classNames.length;i ++  ) ... {
          context.put(
" className " , classNames[i]);

          
/**/ /*  now render the template into a StringWriter  */
          StringWriter writer 
=   new  StringWriter();
          t.merge( context, writer );
          
          writeJavaFile(classNames[i]
+ " Action.java " ,writer.toString());
        
          
/**/ /*  show the World  */
         
//  System.out.println( writer.toString() ); 
          }

    }

    
    
    
private   static   void  writeJavaFile(String name, String str)  ... {
        
try   ... {
            FileWriter fw 
=   new  FileWriter( new  File(name),  true );
            BufferedWriter bw 
=   new  BufferedWriter(fw);

            
//  将读入的字符串写入到文件中
            bw.write(str,  0 , str.length());
            bw.flush();
            bw.close();
            fw.close();
        }
  catch  (IOException e)  ... {
            System.out.println(
" Error-- : "   +  e.toString());
        }

    }


    
private   static   void  loadProperties() ... {
        props 
=   new  Properties();
        
try   ... {
            props.load(Test.
class .getResourceAsStream( " action.properties " ));
        }
  catch  (IOException e)  ... {
            
//  TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    
    
private   static  Object[] prop2List() ... {
        loadProperties();
        System.out.println(props.size());
        
if (props.isEmpty())   return   null ;
        Object[] aa 
=  props.values().toArray();
        
return  aa;
    }

    
    
}




 部分Action.vm代码:

public  ActionForward edit$ ... {className} (ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   
throws  Exception  ... {

  logger.debug(
" --- edit${className} start --- " );
  checkToken(request);
  $
... {className} Form $ ... {className.toLowerCase()} Form  =  ($ ... {className} Form)form;

action.properties:

A
= Jaaas
B
= Jbbbs
C
= Tcccs

采用velocity根据模块自动生成java类


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论