java模版代码生成框架_代码生成框架Velocity

Velocity是一个基于Java的模板引擎,常用于动态页面生成和代码生成。它通过模板语言VTL引用Java对象,模板文件扩展名为`.vm`。在处理过程中,包括初始化引擎、加载模板、创建上下文、赋值和生成代码。示例展示了如何创建模板文件、Java程序以及使用Velocity生成动态内容的过程,提高代码的可维护性。
摘要由CSDN通过智能技术生成

代码生成框架Velocity

Velocity是一个基于Java的模板引擎,用户可以使用模板语言VTL来引用由Java代码定义的对象。

Velocity通常可以作为动态生成页面而广泛使用,还是一种功能强大的代码生成工具。

Velocity模板类似于JSP文件,当客户端发送请求后,Velocity引擎江根据模板产生动态地页面。如果要使用Velocity生成动态页面,需要扩展VelocityServlet类来实现请求的处理,并通过handleRequest方法返回一个模板变量,Velocity会负责模板到页面的转换。

它还可以从模板产生SQL脚本、XML及Java代码等。

1)模板文件

扩展名为“.vm”,是一个文本文件。

2)Java程序

可以为VelocityServlet的子类。

例:

(1)helloworld.vm

None.gif##testassignNone.gif#set($name="gan.shu.man")None.gifEmployee name:$gan.shu.man

None.gif

None.gif##test condition

None.gif#if($name=="gan.shu.man")None.gif$name: very good!!

None.gif#elseNone.gif$name: sorry!!

None.gif#endNone.gif

None.gifProduct information

None.gif##test circular

None.gif#foreach($product in$productList)None.gif$product.Name$$product.Price

None.gif#endNone.gif

None.gif##test programassignNone.gifTotal Price:$$totalPrice

None.gif

(2)HelloWorldVTL.java

None.gifimportjava.io.StringWriter;

None.gifimportjava.io.Writer;

None.gifimportjava.util.ArrayList;

None.gifimportjava.util.Collection;

None.gifimportjava.util.Iterator;

None.gif

None.gifimportorg.apache.velocity.Template;

None.gifimportorg.apache.velocity.VelocityContext;

None.gifimportorg.apache.velocity.app.Velocity;

ExpandedBlockStart.gif

ContractedBlock.gifpublicclassHelloWorldVTL...{

ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicstaticvoidmain(String[] args)throwsException...{

6a9c071a08f1dae2d3e1c512000eef41.png  Velocity.init();

6a9c071a08f1dae2d3e1c512000eef41.png  Template template=Velocity.getTemplate("./src/helloworld.vm");

6a9c071a08f1dae2d3e1c512000eef41.png  VelocityContext ctx=newVelocityContext();

6a9c071a08f1dae2d3e1c512000eef41.png  Collection products=newArrayList();

6a9c071a08f1dae2d3e1c512000eef41.png  products.add(newProduct("Product 1",12.99));

6a9c071a08f1dae2d3e1c512000eef41.png  products.add(newProduct("Product 2",13.99));

6a9c071a08f1dae2d3e1c512000eef41.png  products.add(newProduct("Product 3",11.99));

6a9c071a08f1dae2d3e1c512000eef41.png  ctx.put("productList", products);

6a9c071a08f1dae2d3e1c512000eef41.png  Iterator itr=products.iterator();

6a9c071a08f1dae2d3e1c512000eef41.pngdoubletotal=0.00;

ExpandedSubBlockStart.gif

ContractedSubBlock.gifwhile(itr.hasNext())...{

6a9c071a08f1dae2d3e1c512000eef41.png   Product p=(Product)itr.next();

6a9c071a08f1dae2d3e1c512000eef41.png   total+=p.getPrice();

ExpandedSubBlockEnd.gif  }6a9c071a08f1dae2d3e1c512000eef41.png  ctx.put("totalPrice",newDouble(total));

6a9c071a08f1dae2d3e1c512000eef41.png  Writer writer=newStringWriter();

6a9c071a08f1dae2d3e1c512000eef41.png  template.merge(ctx, writer);

6a9c071a08f1dae2d3e1c512000eef41.png  System.out.println(writer.toString());

ExpandedSubBlockEnd.gif }ExpandedBlockEnd.gif}None.gif

None.gif

(3)Product.java

ExpandedBlockStart.gif

ContractedBlock.gifpublicclassProduct...{

6a9c071a08f1dae2d3e1c512000eef41.pngprivateString name;

6a9c071a08f1dae2d3e1c512000eef41.pngprivatedoubleprice;

ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicProduct(String name,doubleprice)...{

6a9c071a08f1dae2d3e1c512000eef41.pngsuper();

6a9c071a08f1dae2d3e1c512000eef41.pngthis.name=name;

6a9c071a08f1dae2d3e1c512000eef41.pngthis.price=price;

ExpandedSubBlockEnd.gif }ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicString getName()...{

6a9c071a08f1dae2d3e1c512000eef41.pngreturnname;

ExpandedSubBlockEnd.gif }ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicvoidsetName(String name)...{

6a9c071a08f1dae2d3e1c512000eef41.pngthis.name=name;

ExpandedSubBlockEnd.gif }ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicdoublegetPrice()...{

6a9c071a08f1dae2d3e1c512000eef41.pngreturnprice;

ExpandedSubBlockEnd.gif }ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicvoidsetPrice(doubleprice)...{

6a9c071a08f1dae2d3e1c512000eef41.pngthis.price=price;

ExpandedSubBlockEnd.gif }ExpandedBlockEnd.gif}None.gif

输出:

2008-3-10 2:45:12 org.apache.velocity.runtime.log.JdkLogChute log

信息: FileResourceLoader : adding path '.'

2008-3-10 2:45:12 org.apache.velocity.runtime.log.JdkLogChute log

信息: Null reference [template './src/helloworld.vm', line 3, column 16] : $gan.shu.man cannot be resolved.

Employee name: $gan.shu.man

gan.shu.man: very good!!

Product information

Product 1    $12.99

Product 2    $13.99

Product 3    $11.99

Total Price: $38.97

3)处理流程:

使用Velocity生成过程如下:

(1)初始化模板引擎;

(2)加载模板文件;

(3)创建模板上下文;

(4)给模板变量赋值;

(5)替换模板中的值生成代码。

通过模板生成代码是比较好的选择,模板在某种意义上来说就是配置文件的一种,当生成文件内容修改后,用户不需要修改源程序,只需要修改模板文件即可,提高了代码的可维护性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值