Velocity总结资料

本文介绍了 Velocity,一种基于 Java 的模板引擎,适用于 Web 开发中的 MVC 架构。通过示例展示了如何配置 Velocity 并生成包含动态内容的 HTML 页面。此外还详细解释了 Velocity 模板语言 (VTL) 的基本语法。
1 Velocity
Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。 当Velocity应用于web开发时,界面设计人员可以和java程序开发人员同步开发一个遵循MVC架构的web站点,也就是说,页面设计人员可以只 关注页面的显示效果,而由java程序开发人员关注业务逻辑编码。Velocity将java代码从web页面中分离出来,这样为web站点的长期维护提 供了便利,同时也为我们在JSP和PHP之外又提供了一种可选的方案。 Velocity的能力远不止web站点开发这个领域,例如,它可以从模板(template)产生SQL和PostScript、XML,它也可以被当 作一个独立工具来产生源代码和报告,或者作为其他系统的集成组件使用。Velocity也可以为Turbine web开发架构提供模板服务(template service)。Velocity+Turbine提供一个模板服务的方式允许一个web应用以一个真正的MVC模型进行开发。
2 生成代码实例
2.1 添加相关工具包
如图所示:
[img]http://dl.iteye.com/upload/attachment/171258/feb22abe-fd97-3445-9c58-1521621269b3.jpg[/img]

2.2 执行代码
package cn.winjoys.velocity;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

public class VelocityUtil {

/**
* Document Start
*
* Document End
* 2009-11-19 下午03:46:36
* 作者:梦中千万里
*/
public void generator() {
try {
Velocity.init("src/velocity.properties");

VelocityContext context = new VelocityContext();
context.put("list", getDatas());
context.put("author", "梦中千万里");
context.put("date", new Date());

Template template = Velocity.getTemplate("template/simple.vm");

// out
BufferedWriter writer = writer = new BufferedWriter(
new OutputStreamWriter(System.out));
if (template != null)
template.merge(context, writer);

writer.flush();
writer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* Document Start
*
* Document End
* 2009-11-19 下午03:46:39
* 作者:梦中千万里
* @return
*/
public List getDatas() {
List<Object> list = new ArrayList<Object>();
list.add("First Object");
list.add(new Date());
list.add(100);
return list;
}
}
2.3 模版文件
The first demo[$author][$date]
#foreach( $name in $list )
the $name is $name.class!
#end
2.4 输出结果
The first demo[梦中千万里][Thu Nov 19 16:09:53 CST 2009]
the First Object is class java.lang.String!
the Thu Nov 19 16:09:53 CST 2009 is class java.util.Date!
the 100 is class java.lang.Integer!

3 Velocity Template Language (VTL)
3.1 变量
$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ } ]
例如
简化形式:$author
完整形式:${author}
3.2 属性
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[a..z, A..Z ][ a..z, A-Z, 0..9, -, _ ]* [ } ]
例如
$name.class
3.3 方法
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]*( [ optional parameter list... ] ) [ } ]
例如
$name.getClass()
3.4 设置
# [ { ] set [ } ] ( $ref = [ ", ' ]arg[ ", ' ] )
例如
• Variable reference: #set( $monkey = $bill )
• String literal: #set( $monkey.Friend = 'monica' )
• Property reference: #set( $monkey.Blame = $whitehouse.Leak )
• Method reference: #set( $monkey.Plan = $spindoctor.weave($web) )
• Number literal: #set( $monkey.Number = 123 )
• Range operator: #set( $monkey.Numbers = [1..3] )
• Object list: #set( $monkey.Say = ["Not", $my, "fault"] )
• Object map: #set( $monkey.Map = {"banana" : "good", "roast beef" : "bad"})
3.5 条件语句
# [ { ] if [ } ] ( [condition] ) [output] [ # [ { ] elseif [ } ] ( [condition] ) [output] ]* [ # [ { ] else [ } ] [output] ] # [ { ] end [ } ]
Operator Name Symbol Alternative Symbol Example
Equals Number == eq #if( $foo == 42 )
Equals String == eq #if( $foo == "bar" )
Object Equivalence == eq #if( $foo == $bar )
Not Equals != ne #if( $foo != $bar )
Greater Than > gt #if( $foo > 42 )
Less Than < lt #if( $foo < 42 )
Greater Than or Equal To >= ge #if( $foo >= 42 )
Less Than or Equal To <= le #if( $foo <= 42 )
Boolean NOT ! not #if( !$foo
3.6 循环语句
# [ { ] foreach [ } ] ( $ref in arg ) statement # [ { ] end [ } ]
例如
#foreach( $name in $list )
the $name is $name.getClass()!
#end
3.7 引用文件
# [ { ] include [ } ] ( arg[ arg2 ... argn] )
例如
#include("template/temp.txt")
说明
将文件temp.txt里的内容拷贝到生成的代码中,但此时不解析执行。

# [ { ] parse [ } ] ( arg )
例如
#parse( "lecorbusier.vm" )
说明
将文件temp.txt里的内容拷贝到生成的代码中,但此时解析执行。
3.8 停止模版引擎
# [ { ] stop [ } ]
主要是用于调试
例如
The first demo[${author}][$date]
#stop
#foreach( $name in $list )
the $name is $name.getClass()!
#end

#include("template/temp.txt")
不会执行
#foreach( $name in $list )
the $name is $name.getClass()!
#end
但会执行
#include("template/temp.txt")

3.9 计算
# [ { ] evaluate [ } ] ( arg )
例如
#evaluate( 'string with VTL #if(true)will be displayed#end' )
将执行字符串
3.10 定义索引块
# [ { ] define [ } ] ( $ref ) statement # [ { ] end [ } ]
例如
#define( $hello ) Hello $who #end #set( $who = "World!") $hello ## displays Hello World!
3.11 定义宏
# [ { ] macro [ } ] ( vmname $arg1 [ $arg2 $arg3 ... $argn ] ) [ VM VTL code... ] # [ { ] #end [ } ]
3.12 注释
## This is a comment.
多行采用如下形式
#*
This is a multiline comment.
This is the second line
*#
4 资源
【1】http://velocity.apache.org/index.html
采用PyQt5框架与Python编程语言构建图书信息管理平台 本项目基于Python编程环境,结合PyQt5图形界面开发库,设计实现了一套完整的图书信息管理解决方案。该系统主要面向图书馆、书店等机构的日常运营需求,通过模块化设计实现了图书信息的标准化管理流程。 系统架构采用典型的三层设计模式,包含数据存储层、业务逻辑层和用户界面层。数据持久化方案支持SQLite轻量级数据库与MySQL企业级数据库的双重配置选项,通过统一的数据库操作接口实现数据存取隔离。在数据建模方面,设计了包含图书基本信息、读者档案、借阅记录等核心数据实体,各实体间通过主外键约束建立关联关系。 核心功能模块包含六大子系统: 1. 图书编目管理:支持国际标准书号、中国图书馆分类法等专业元数据的规范化著录,提供批量导入与单条录入两种数据采集方式 2. 库存动态监控:实时追踪在架数量、借出状态、预约队列等流通指标,设置库存预警阈值自动提醒补货 3. 读者服务管理:建立完整的读者信用评价体系,记录借阅历史与违规行为,实施差异化借阅权限管理 4. 流通业务处理:涵盖借书登记、归还处理、续借申请、逾期计算等标准业务流程,支持射频识别技术设备集成 5. 统计报表生成:按日/月/年周期自动生成流通统计、热门图书排行、读者活跃度等多维度分析图表 6. 系统维护配置:提供用户权限分级管理、数据备份恢复、操作日志审计等管理功能 在技术实现层面,界面设计遵循Material Design设计规范,采用QSS样式表实现视觉定制化。通过信号槽机制实现前后端数据双向绑定,运用多线程处理技术保障界面响应流畅度。数据验证机制包含前端格式校验与后端业务规则双重保障,关键操作均设有二次确认流程。 该系统适用于中小型图书管理场景,通过可扩展的插件架构支持功能模块的灵活组合。开发过程中特别注重代码的可维护性,采用面向对象编程范式实现高内聚低耦合的组件设计,为后续功能迭代奠定技术基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值