簡體   English   中英

是否可以通過在Apache Velocity中定義模板來生成Java代碼?

[英]Is it possible to generate a Java code by defining a template in Apache Velocity?

我需要使用Velocity Template概念生成Java文件。 請您指導一下。

Java代碼應包含一些方法,導入和變量...

謝謝,伊斯瓦爾

對於Velocity / Java部分,您或多或少需要這樣做:

// factory and an engine instance
VelocityEngineFactory velocityEngineFactory = new VelocityEngineFactory();
VelocityEngine engine = velocityEngineFactory.createVelocityEngine();
// now you need to give the variables you wanna have access from velocity script
VelocityContext context = new VelocityContext(properties);

ByteArrayOutputStream temp = new ByteArrayOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(temp));
// generate the result, where scriptString is the velocity script
engine.evaluate(context, bufferedWriter, null, scriptString);
bufferedWriter.flush();
textResultant = temp.toString();

因此,您可以創建一個腳本,對其進行加載並以編程方式對其進行處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM