velcoity使用说明:velocity基础

来源:互联网 发布:紫峰抢票软件 编辑:程序博客网 时间:2024/05/20 04:50

概述:

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

比较重要的几个概念:

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

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

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

3.1.引用(reference):java代码中put到Velocity的变量替换符,有三种引用(reference):变量($variable)、属性($variable.attribute)、方法($variable.method())

3.2.指令(directives):指令主要是允许页面设计人员进行变量和脚本控制,以及自定义变量,常见的指令有:

循环指令:# [ { ] foreach [ } ] ( $ref in arg ) statement # [ { ] end [} ]

条件判断指令:# [ { ] if [ } ] ( [condition] ) [output] [ # [ { ] elseif [ } ] ( [condition] ) [output] ]* [ # [ { ] else [ } ] [output] ] # [{ ]end [ } ]

包含指令:# [ { ] include [ } ] ( arg[ arg2 ... argn] )

3.3.宏定义(macros):当重复vm模板代码中存在着一些共性的代码,就可以自定义一个宏,减少代码量



 

演示代码:

HelloWorld.java

[java] view plaincopy
  1. package com.wm.mad.tmp;  
  2.   
  3. import java.io.StringWriter;  
  4.   
  5. import org.apache.velocity.Template;  
  6. import org.apache.velocity.VelocityContext;  
  7. import org.apache.velocity.app.VelocityEngine;  
  8.   
  9. import org.apache.log4j.Logger;  
  10. import org.apache.log4j.PropertyConfigurator;  
  11.   
  12. public class HelloWorld {  
  13.     static Logger logger = Logger.getLogger(HelloWorld.class);  
  14.       
  15.     public static void main(String[] args) throws Exception {  
  16.         PropertyConfigurator.configure("log4j.properties");  
  17.           
  18.         VelocityEngine ve = new VelocityEngine();  
  19.         ve.init("velocity.properties");  
  20.   
  21.         Template template = ve.getTemplate("helloWorld.vm");  
  22.   
  23.         VelocityContext context = new VelocityContext();  
  24.   
  25.         context.put("name""madding");  
  26.         context.put("password""madding");  
  27.   
  28.         StringWriter writer = new StringWriter();  
  29.   
  30.         template.merge(context, writer);  
  31.   
  32.         System.out.println(writer.toString());  
  33.         if(logger.isInfoEnabled()) {  
  34.             logger.info("operator is success");  
  35.         }  
  36.     }  
  37. }  

 

helloWorld.vm:

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

 

velocity.properties:

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

 

log4j.properties:

[xhtml] view plaincopy
  1. log4j.rootLogger=INFO,logfile  
  2.   
  3. log4j.appender.logfile=org.apache.log4j.RollingFileAppender  
  4. log3j.appender.logfile.Threshold=INFO  
  5. log4j.appender.logfile.File=d:/log/velocity_use.log  
  6. log4j.appender.logfile.Append=true  
  7. log4j.appender.logfile.MaxFileSize=1MB  
  8. log4j.appender.logfile.MaxBackupIndex=3  
  9. log4j.appender.logfile.layout=org.apache.log4j.PatternLayout  
  10. log4j.appender.logfile.layout.ConversionPattern =%d %p [ %c] - %m %n %d 
原创粉丝点击