已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
velocity一些用法(不是uesrguide)近期研究一下eclipse的代码生成.在两个模板中间决择.网上一查发现全是说FREEMARKER好的.而且还拿出来强有例的对比的表格.但是,对比的对象velocity基本上就是1.0的初初级版.这个对比.简直就好象是韦小宝学习韦陀掌的招式,来跟普通的少林弟子过招,就算是人家用长拳,韦小宝当然也打不过啦.有点扯远了呵呵Velocity还是一个不错的开源项目.其实,一般开源项目,只要持续的更新.那么就算开始的时候多么的垃圾多么的不完善.也终会成大器的.hibernate是这样,mysql 是这样.网上写的例子一般都是TVM的模板.而且还是有些例子完成不能运行的这些不说了.大家直接看velocity 官网的userguide就好了.比网上写的还全.而且主要是人家跟最新的velocity是对应的,是可以运行的.上来先来一个大家都常见的就是把一个list里的东西,转成aaa, bbb , ccc 注意最后的这个没有”,” 我记得我以前也是被这个问题困扰了好久. #foreach 会生成包含$products 中对象的一个列表. 每一次循环都会将列表中的一个对象赋与变量$product .$allProducts 或以是一个 Vector, a Hashtable or an Array 类型的容器. 指定给变量 $product 是一个引用到其中一个 java 对象的引用. For example, if $product 确实是一个 java 代码中的 Productclass i,它可以这样的方法访问$product.Name method (ie: $Product.getName().我们假设 $allProducts 是一个 Hashtable.看看取出其中的东东多么简单:#foreach( $key in $allProducts.keySet() )Key: $key - Value: $allProducts.get($key)#end通过引用变量$velocityCount 可以访问到 Velocity 提供的计数器:#foreach( $customer in $customerList )$velocityCount$customer.Name#end但是官网上的例子这样写道:Velocity provides an easy way to get the loop counter so that you can do something like the following: #foreach( $customer in $customerList ) $foreach.count$customer.Name#end这个小例子.真的是憋倒一堆人呀.还以为在freemarker里好实现在vm里不好写呢.结果,这么容易. Velocity also now provides an easy way to tell if you are on the last iteration of a loop: #foreach( $customer in $customerList ) $customer.Name#if( $foreach.hasNext ),#end#end为什么让这个有名的小例子全面运行.给一个完整的TEST吧./* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * License); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * /licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */import org.apache.velocity.app.Velocity;import org.apache.velocity.VelocityContext;import org.apache.velocity.Template;import org.apache.velocity.exception.ParseErrorException;import org.apache.velocity.exception.ResourceNotFoundException;import java.io.*;import java.util.ArrayList;/* * This class is a simple demonstration of how the Velocity Template Engine * can be used in a standalone application. * * author Jason van Zyl * author Geir Magnusson Jr. * version $Id: Example.java 463298 2006-10-12 16:10:32Z henning $ */public class Example public Example(String templateFile) try Velocity.init(perties);/ 直接用Velocity.init() 也可以. VelocityContext context = new VelocityContext(); context.put(list, getNames(); Template template = null; try template = Velocity.getTemplate(templateFile); catch( ResourceNotFoundException rnfe ) System.out.println(Example : error : cannot find template + templateFile ); catch( ParseErrorException pee ) System.out.println(Example : Syntax error in template + templateFile + : + pee ); BufferedWriter writer = writer = new BufferedWriter( new OutputStreamWriter(System.out); if ( template != null) template.merge(context, writer); writer.flush(); writer.close(); catch( Exception e ) System.out.println(e); public ArrayList getNames() ArrayList list = new ArrayList(); list.add(ArrayList element 1); list.add(ArrayList element 2); list.add(ArrayList element 3); list.add(ArrayList element 4); return list; public static void main(String args) Example t = new Example(example.vm); VM.文件.#set( $this = Velocity)$this is great!#foreach( $name in $list ) $name #if( $foreach.hasNext ),#end#end#set( $condition = true)#if ($condition) The condition is true!#else The condition is false!#endvelocity还能变态的支持XML文件.SAXBuilder builder; Document root = null; try builder = new SAXBuilder(); root = builder.build(test.xml); catch( Exception ee) System.out.println(Exception building Document : + ee); return; /* * now, make a Context object and populate it. */ VelocityContext context = new VelocityContext(); context.put(root, root); /* * make a writer, and merge the template against the context */ Template template = Velocity.getTemplate(templateFile); writer = new BufferedWriter(new OutputStreamWriter(System.out); template.merge( context , writer);这样就可以了.#macro ( recursive $e $indent )#if( $e.getChildren().size() 0 )$indent #foreach ($child in $e.getChildren() )#recursive( $child $indent )#end$indent #else$indent $indent $e.getTextTrim()$indent #end#end#set($i = )First, we print out the document tree with arecursive 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).getChild(full).getText()这个写法.也挺疯的.附上XML文件. TeamT Velocity The Velocity Project I am the body 还支持breakIf you want to stop looping in a foreach from within your template, you can now use the #break directive to stop looping at any time: # list first 5 customers only#foreach( $customer in $customerList ) #if( $foreach.count 5 ) #break #end $customer.Name#endvelocity 必杀技.:The #evaluate directive can be used to dynamically evaluate VTL. This allows the template to evaluate a string that is created at render time. Such a string m
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2026广东佛山市南海区丹灶镇金沙中学度短期代课教师招聘3人笔试备考试题及答案解析
- 2026浙江杭州市西湖区闻裕顺学前教育集团诚聘教师(非事业)2人笔试备考试题及答案解析
- 2026浙江杭州市西湖区三墩镇中心幼儿园诚聘幼儿教师(非事业)1人笔试备考题库及答案解析
- 2026广东阳江市阳春市高校毕业生就业见习招募29人(第三期)笔试备考题库及答案解析
- 2026江苏南京大学地理与海洋科学学院技术管理招聘笔试备考试题及答案解析
- 2026浙江宁波市余姚市阳明街道办事处招聘编外工作人员1人笔试备考试题及答案解析
- 2026滁州市年明光市抹山小学招募小学语文、英语见习顶岗教师笔试备考题库及答案解析
- 2026福建厦门市集美区侨英小学产假顶岗教师招聘1人笔试备考题库及答案解析
- 北京市东城区2025-2026学年高三上学期期末考试语文试卷及参考答案
- 4.7.3 用药与急救(第二课时)教学设计-2025-2026学年人教版生物(2024)八年级上册
- 高速公路安全培训课件
- GB/T 12229-2025通用阀门碳素钢铸件技术规范
- (零模)2026届广州市高三年级调研测试数学试卷(含答案解析)
- 2026年辽宁医药职业学院单招职业技能测试题库带答案解析
- GB/T 13471-2025节能项目经济效益计算与评价方法
- 2024 年9月8日江西省“五类人员”选拔(事业编转副科)笔试真题及答案解析
- 安全保卫技师职称培训课件
- 营养与高脂血症46课件
- 肿瘤病人恶心呕吐的护理
- 出租车代班合同(2篇)
- 物业小区安全生产隐患排查治理表
评论
0/150
提交评论