《Velocity介绍》PPT课件.ppt_第1页
《Velocity介绍》PPT课件.ppt_第2页
《Velocity介绍》PPT课件.ppt_第3页
《Velocity介绍》PPT课件.ppt_第4页
《Velocity介绍》PPT课件.ppt_第5页
已阅读5页,还剩28页未读 继续免费阅读

下载本文档

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

文档简介

Velocity 模板介绍,1.Velocity概述 ?,Velocity是一个基于java的模板引擎(template engine:模板引擎的作用就是取得数据并加以处理,最后显示出数据 )。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。,2.Velocity的应用领域,Web应用的开发。 作为模板产生SQL,XML或代码等。 作为其他系统的集成组件使用,3. Velocity工作流程和原理,当Velocity应用于application program或 a servlet,你通常将做下面的事情 初始化Velocity 创建Context对象 添加数据到Context 选择模板 合并模板和数据产生输出页面,3.1Velocity原理举例说明,public static void main(String args) throws Exception Velocity.init(); VelocityContext context = new VelocityContext(); context.put(“name“, “Velocity“); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( System.out); Template template = Velocity.getTemplate(“src/velocity/hello.vm“); template.merge(context, writer); writer.flush(); writer.close(); ,3.1Velocity原理举例说明,Velocity 文件(hello.vm) hello $name; 这个例子的输出效果为: Hello Velocity,4. VTL介绍,VTL提供一种简单、容易和干静的方法将动态内容合并到Web页面。VTL使用引用(references)将动态内容插入到Web页面中。变量是一种引用,可以指向Java代码中的定义内容,或者由Web页面中的VTL语句来获得值。 例如:#set( $a = “Velocity“ ) VTL语句以#开头,并包含指令(set)。变量以$开头,用引号引起 VTL语法包括: 1,注释 2,引用(References) 3,指令(Directives),4.1VTL-注释,VTL支持单行注释(以#开始)和多行注释(包括在#*和*#之间),下面是一个例子: # This text is not visible. #* This text, as part of a multi-line comment, is not visible. This text is not visible; it is also part of the multi-line comment. This text still not visible. *#,4.2VTL-引用(References),VTL有3种类型的引用:变量、属性和方法。 (1)变量 变量的格式:$VTL标识符 VTL标识符以字母开始,由字母、数字、横划线(-)或下划线(_)组成。 例如下面的例子: #set( $foo = “gibbous“ ) $moon = $foo 输出结果是:$moon = gibbous,4.2VTL-引用(References),(2)属性 属性的格式:$VTL标识符. VTL标识符 下面是属性引用的例子: $customer.Address $purchase.Total 拿第一例子来说,有两种意思: 返回Hashtable对象customer中键值 为Address的值 $customer.getAddress()方法引用的缩 写(JavaBean属性的getter方法) 至于是哪种情况,Velocity会做决定,返回合适的值。,4.2VTL-引用(References),(3)方法 方法的格式:$VTL标识符(参数列表) 下面是方法引用的例子: $customer.getAddress() $purchase.getTotal() $page.setTitle( “My Home Page“ ) $person.setAttributes( “Strange“, “Weird“, “Excited“ ),4.3VTL-指令(Directives),详解以下VTL常用指令: #set #if / #elseif / #else 循环:foreach #include #parse #stop #macro,4.3.1VTL-#set(1),(1)#set 格式:#set( LHS = RHS ) LHS可以是变量引用或属性引用 RHS可以是引用、字符串、数字、ArrayList或Map 例如: #set( $monkey = $bill ) # variable reference #set( $monkey.Friend = “monica“ ) # string literal #set( $monkey.Blame = $whitehouse.Leak ) # property reference #set( $monkey.Number = 123 ) #number literal #set( $monkey.Say = “Not“, $my, “fault“ ) # ArrayList,4.3.1VTL-#set,(1)#set RHS可以是简单的算术表达式 如果RHS的结果为null,是不会赋值给LHS的 String文字可以使用双引号或单引号括起。两者的主要区别是双引号中的引用会替换成相应的值,而单引号中的引用原样输出 例如: #set( $value = $foo + 1 ) # Addition #set( $directoryRoot = “www“ ) #set( $templateName = “index.vm“ ) #set($template= $directoryRoot/$templateName“ ) $template 输出结果是:www/index.vm,4.3.2VTL-#if#else,(2)#if / #elseif / #else #if指令在条件成立时,显示#if和#end之间的内容,否则显示#else和#end之间的内容。下面是一个例子: #if( $foo ) Velocity! #end 条件成立有两种情况: 如果$foo是boolean,则$foo要为true; 否则,$foo不为null #if指令中可以使用的关系和逻辑符号包括: =、 &(and)、|(or)、!(not),4.3.3VTL-#foreach,(3)#foreach #foreach( $product in $allProducts ) $product #end $allProducts的内容可以是Vector、Hashtable或ArrayList,每次取出一个值赋值给$product,4.3.4VTL-#include,(4)#include #include指令导入本地文件到#include指令定义的地方。导入的文件内容不会被模板引擎解析。出于安全考虑,导入的文件应该放在TEMPLATE_ROOT目录下。一次可以导入多个文件,文件名之间用逗号分隔;并且通常使用变量引用来替代文件名。下面是一个例子: #include(“greetings.txt“, seasonalstock ),4.3.5VTL- #parse,(5)#parse #parse指令允许导入一个包含VTL的本地文件,并由模板引擎进行解析。 例如: #parse( “parsefoo.vm“ ),4.3.6VTL-#stop,(6)#stop #stop指令停止模板引擎的执行并返回。这在Debug时很有用。,4.3VTL-#macro,(7)#macro #macro指令允许定义一段重复使用的VTL模板(称Velocimacros)。 Velocimacros可以有0或多个参数。例如: #macro( tablerows $color $somelist ) #foreach( $something in $somelist ) $something #end #end tablerows的调用: #set($greatlakes = “Superior“,“Erie“ ) #set( $color = “blue“ ) #tablerows( $color $greatlakes ),5.Velocity的国际化,Velocity本身支持模版的国际化编码转换,看看Velocity提供的方法: Public Template getTemplate (String tem String encoding) 另外,在Velocity的tools:MessageTool, 提供了变量text包含国际化标签,这样只需要简单的编写标签代码即可,如:$text.get(title).,6. Velocity对Framework的支持,VelocityTools/VelocityViewServlet 对Servlet的支持,使用 VelocityViewServlet来代替 Servlet Velocity Tools / VelocityStruts 对Struts的支持. Third party frameworks 对第三方框架的支持,比如:Spring,Turbine等,6. Velocity实例开发,实例的功能 实现用Velocity对XML文件的输出。 XML文件结构,6.1. Velocity实例开发,Test.xml Team Velocity The Velocity Project this is a test ,6.2. Velocity实例开发,实现效果,6.3. Velocity实例开发,xml.vm #macro ( recursive $e $indent ) #if( $e.getChildren().size() 0 ) $indent   “),6.4. Velocity实例开发,xml.vm XML First, we print out the document tree with a recursive Velocimacro : #recursive( $root.getRootElement() $i ) Next, we access pieces of data directly : email : $root.getRootElement().getChild(“properties“).getChild(“author“).getChild(“email“).getText() last name : $root.getRootElement().getChild(“properties“).getChild(“author“).getChild(“name“).getChild(“last“).getText() ,6.5. Velocity实例开发,Servlet的开发 public class Testve extends VelocityServlet public Template handleRequest(HttpServletRequest request,HttpServletResponse response, Context ctx)throws Exception Template outty = null; String pa = request.getRealPath(“/“); Velocity.init(); SAXBuilder builder; Document root = null; builder = new SAXBuilder(); root = builder.build(pa + “test.xml“); ctx.put(“root“, root); outty = getTemplate(“/templates/xml.vm“); return outty; ,6.6. Velocity实例开发,Web.xml Testve sample.servlet.Testve Testve /Testve ,7. Velocity的优点和缺点,Velocity的优点 简单的模板语法 实现对内容和显示方式的分离 Velocity的缺点 异常处理以及日志上,8. Velocity VS XSLT,Velocity的优势在于这种技术对用户后台 Java 代码侵入性非常低,可以与其它 Template 可以任意替换,而不影响用户后台 Java 代码。另外,Velocity语法很少,简单易学。 XSLT的最大优势就在于能XML的超强处理。,9. 总结,Velocity 解决了如何在 Servlet 和 网页之间传递数据的问题。当然这种传输数据的机制

温馨提示

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

评论

0/150

提交评论