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

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
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值