六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 72|回复: 0

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

[复制链接]

升级  17.33%

18

主题

18

主题

18

主题

秀才

Rank: 2

积分
76
 楼主| 发表于 2013-2-5 01:20:26 | 显示全部楼层 |阅读模式
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">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);

          
/**//* now render the template into a StringWriter */
          StringWriter writer 
= new StringWriter();
          t.merge( context, writer );
          
          writeJavaFile(classNames
+"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
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表