减法运算
#set($page=$page-1) ##这条语句会出错,改成如下:
#set($page=$page- 1) ##在减号前后各加一个空格
在做分页处理的时候,这个地方老报错,我快晕倒了,最后终于找到了问题,希望使用velocity的同仁看到后,能更好的使用了。
判断字符是否相等
#if($a==$b) <div>success</div>#end ##如果$a或$b为空,这条语句有语法错误,改成如下:
#if("$a"=="$b") <div>success</div>#end 或者
#if($a&&$b&$a==$b) <div>success</div>#end
javascript中使用vm变量
<script>
#if($a&&$a.length()>0)
var a="$a"
alert(a);
#end
</script>
foreach循环的counter(循环次数计数)
在velocity.properties中有一段配置
directive.foreach.counter.name = velocityCount ##定义变量名称
directive.foreach.counter.initial.value = 1 ##定义计算从1开始累加
$velocityCount
模板的解析
1.模板文件的merge
StringWriter writer = new StringWriter();
Map context=new HashMap();
context.put("vm_prop1","12");
VelocityContext model = new VelocityContext(context);
model.put("vm_prop2", "14");
try {
Template tpl = Velocity.getTemplate("portal.vm");
tpl.merge(model, writer);
writer.close();
} catch (Exception e) {
log.error("Can't merge portal.vm", e);
}
log.info(writer.toString);
对portal.vm中的vm_prop1和vm_prop2两个变量赋值并解析返回writer
2.字符串的merge
public static boolean evaluate(org.apache.velocity.context.Context context, Writer writer, String logTag, Reader reader) throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException {
SimpleNode nodeTree = null;
try {
nodeTree = RuntimeSingleton.parse(reader, logTag, false);
} catch (ParseException pex) {
throw new ParseErrorException(pex.getMessage());
}
if (nodeTree != null) {
InternalContextAdapterImpl ica = new InternalContextAdapterImpl(context);
ica.pushCurrentTemplateName(logTag);
try {
try {
nodeTree.init(ica, RuntimeSingleton.getRuntimeServices());
} catch (Exception e) {
RuntimeSingleton.error("Velocity.evaluate() : init exception for tag = " + logTag + " : " + e);
}
nodeTree.render(ica, writer);
} finally {
ica.popCurrentTemplateName();
}
return true;
}
return false;
}
StringReader reader = new StringReader("#foreach($n in $list) <ul><li>$n.Id</li></ul>#end");
reader为读取一个vm文件里的内容,包含vm表达式,logTag为这一串字符串赋予一个模板名称(如:"a.vm")通过context对vm表达式赋值,并对reader读取的字符串进行解析,返回给writer
servlet应用
1.可增加一个Listener,在这个Listener初始化velocity的一些配置,这样避免每个servlet都需要初始化velocity
Properties props = new Properties();
try {
File conf = new File(event.getServletContext().getRealPath("WEB-INF/velocity.properties"));
props.load(new FileInputStream(conf));
}catch (Exception e) {
e.printStackTrace();
}
try {
Iterator<Object> iter = props.keySet().iterator();
while(iter.hasNext()) {
String key = (String) iter.next();
Velocity.setProperty(key,props.getProperty(key));
}
Velocity.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
"org.apache.velocity.runtime.log.SimpleLog4JLogSystem" );
Velocity.setProperty("runtime.log.logsystem.log4j.category", "sys.velocity");
Velocity.setProperty("resource.loader","file");
Velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, event.getServletContext().getRealPath("/WEB-INF/templates"));
Velocity.setProperty("parser.pool.size","3");
Velocity.setProperty("velocimacro.library","macro_default.vm,macro_expopo.vm");
Velocity.init();
} catch (Exception e) {
e.printStackTrace();
}
原文章地址:http://hi.baidu.com/anct125/blog/item/d2c600098f4178236a60fb60.html