代码生成架构Velocity_第1页
代码生成架构Velocity_第2页
代码生成架构Velocity_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

代码生成框架代码生成框架 VelocityVelocity Velocity 是一个基于 Java 的模板引擎 用户可以使用模板语言 VTL 来引用由 Java 代码定义的对象 Velocity 通常可以作为动态生成页面而广泛使用 还是一种功能强大的代码生成工具 Velocity 模板类似于 JSP 文件 当客户端发送请求后 Velocity 引擎江根据模板产生动态地页面 如果要使用 Velocity 生成动态页面 需要扩展 VelocityServlet 类来实现请求的处理 并通过 handleRequest 方法返回一个模板变量 Velocity 会负责模板到页面的转换 它还可以从模板产生 SQL 脚本 XML 及 Java 代码等 1 模板文件 扩展名为 vm 是一个文本文件 2 Java 程序 可以为 VelocityServlet 的子类 例 1 helloworld vm Java 代码 1 test assign 2 set name gan shu man 3 Employee name gan shu man 4 5 test condition 6 if name gan shu man 7 name very good 8 else 9 name sorry 10 end 11 12 Product information 13 test circular 14 foreach product in productList 15 product Name product Price 16 end 17 18 test program assign 19 Total Price totalPrice 2 HelloWorldVTL java Java 代码 1 import java io StringWriter 2 import java io Writer 3 import java util ArrayList 4 import java util Collection 5 import java util Iterator 6 7 import org apache velocity Template 8 import org apache velocity VelocityContext 9 import org apache velocity app Velocity 10 public class HelloWorldVTL 11 public static void main String args throws Exception 12 Velocity init 13 Template template Velocity getTemplate src helloworld vm 14 VelocityContext ctx new VelocityContext 15 Collection products new ArrayList 16 products add new Product Product 1 12 99 17 products add new Product Product 2 13 99 18 products add new Product Product 3 11 99 19 ctx put productList products 20 Iterator itr products iterator 21 double total 0 00 22 while itr hasNext 23 Product p Product itr next 24 total p getPrice 25 26 ctx put totalPrice new Double total 27 Writer writer new StringWriter 28 template merge ctx writer 29 System out println writer toString 30 31 3 Product java Java 代码 1 public class Product 2 private String name 3 private double price 4 public Product String name double price 5 super 6 this name name 7 this price price 8 9 public String getName 10 return name 11 12 public void setName String name 13 this name name 14 15 public double getPrice 16 return price 17 18 public void setPrice double price 19 this price price 20 21 输出 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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论