velcoity基础

概述:

velocity是一个典型的model和view分离设计思想的体现,因此其概念定义的几个主要元素和mvc中的概念也有一定的隐射关系。

比较重要的几个概念:

1.VelocityContext:velocity上下文,用来保存数据用的,主要是工程师书写代码是放数据用的,提供给模板使用

2.Template:模板,设计师设计的页面的加载器,具体的数据则由VelocityContext中获取,template.merge是将数据和vm模板合并输出具体的结果

3.vm模板:设计师设计的页面,包含样式以及替换的数据标识等内容,具体内容如下:

1、变量的定义、赋值与使用规则:赋值的左边必须是一个变量($name)或属性引用($member.Sex),右值为以下六种类型之一:且如果右值为null,则不进行赋值操作,即左值还是保留以前的值;
变量引用:#set($member.Sex=$sex)
字面字符串:#set($username="hzd")
属性引用:#set($color=$ball.Color)
变量引用:#set($member.Age=$ager.sum($age/123/...))
字面数字:#set($money=123456)
数组列表:#set($arr=["not",$m,"xxxx"])
velocity模板中未被定义的变量将被认为是一个字符串:
#set($name="hzd")
$newname=$name,则输出为$newname=hzd
velocity模板中不会将reference解释为对象的实例变量:
如:$foo.Name将被解释为foo对象的getName()方法,而不是foo对象的Name实例变量,
$data.Request.ServerName等同于$data.getRequest.getServerName()
2、遍历循环规则:语法#foreach ...#end,$velocityCount为当前循环的索引(可以通过在velocity配置文件中更改该名称).
循环数组: #foreach($item in ["one","two","three"]) $item #end ,将输出one two three
循环List:#foreach($item in $memberList) $item.name #end,item代表列表中的对象,如果item为值类型(int等),则可以直接使用$item输出值,如果为引用类型(String,User,...),则可以使用$item.name引用实例变量或$item.xxx()调用对象的方法
循环Map:#foreach($key in $memberMap.keySet() ) key:$key;value:$memberMap.get($key) #end
3、条件:使用#if #elseif #else #end 指令可以帮助决定程序的执行流程,如果$foo是boolean,则$foo要为true,否则,$foo不为null

判断对象是否为null的方法:
(1)、#if($member),不为空;#if(!$member),为空;
(2)、#ifnull() 、#ifnotnull():如,ifnull ($foo),要使用这个特性必须在velocity.properties文件中加入:


演示代码:

HelloWorld.java

[java:nogutter] view plain copy
  1. packagecom.wm.mad.tmp;
  2. importjava.io.StringWriter;
  3. importorg.apache.velocity.Template;
  4. importorg.apache.velocity.VelocityContext;
  5. importorg.apache.velocity.app.VelocityEngine;
  6. importorg.apache.log4j.Logger;
  7. importorg.apache.log4j.PropertyConfigurator;
  8. publicclassHelloWorld{
  9. staticLoggerlogger=Logger.getLogger(HelloWorld.class);
  10. publicstaticvoidmain(String[]args)throwsException{
  11. PropertyConfigurator.configure("log4j.properties");
  12. VelocityEngineve=newVelocityEngine();
  13. ve.init("velocity.properties");
  14. Templatetemplate=ve.getTemplate("helloWorld.vm");
  15. VelocityContextcontext=newVelocityContext();
  16. context.put("name","madding");
  17. context.put("password","madding");
  18. StringWriterwriter=newStringWriter();
  19. template.merge(context,writer);
  20. System.out.println(writer.toString());
  21. if(logger.isInfoEnabled()){
  22. logger.info("operatorissuccess");
  23. }
  24. }
  25. }

helloWorld.vm:

[xhtml:nogutter] view plain copy
  1. 你的
  2. 名字是:$name
  3. 密码是:$password

velocity.properties:

[xhtml:nogutter] view plain copy
  1. input.encoding=GBK
  2. output.encoding=GBK
  3. velocimacro.permissions.allow.inline=true
  4. runtime.log=d:/log/velocity.log

转自:http://blog.csdn.net/madding/article/details/4604414

http://gourmand.iteye.com/blog/1555322

http://blog.csdn.net/benbenming/article/details/886668

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值