velocity学习笔记

来源:互联网 发布:黄酒 推荐 知乎 编辑:程序博客网 时间:2024/05/24 06:41
velocity小计
what:velocity是一个向客户的显示数据的一种很方便的语言,类似与jsp.velocity文件(在html中插入符合velocity语法的文件)是在服务器端运行的,通过velocity引擎(一个解释
template语法的java库)翻译,输出html文件,发送到客户端。通俗的说velocity是把客户请求的数据插入到合适的html文本中的一种工具。

how:velocity原理是把数据放入到一个map中,这个map通过引用被velocity引擎拿到,在引擎运行是,根据在html文本中插入的velocity语句,引擎通过$标示的变量,拿到需要的值,
填充到合适的位置。

app:References begin with $ and are used to get something. Directives begin with # and are used to do something.
变量以$开头作用是为了获得值,指令是以#开头,作用是去做一些动作。

一下是常用指令和例子

1、$!obj  直接返回对象结果。
   如:在html标签中显示java对象msg的值。<p>$!msg</p>
  在html标签中显示经过HtmlUtil对象处理过后的msg对象的值  <p>$!HtmlUtil.doSomething($!msg)</p>

2、#if($!obj) #else #end 判断语句
   如:在EasyJWeb各种开源应用中,我们经常看到的用于弹出提示信息msg的例子。
   #if($msg)
   <script>
   alert('$!msg');
   </script>
   #end
上面的脚本表示当对象msg对象存在时,输出<script>等后面的内容。
3、#foreach( $info in $list) $info.someList #end  循环读取集合list中的对象,并作相应的处理。
   如:EasyJF开源论坛系统中论(0.3)坛首页显示热门主题的html界面模板脚本:
  #foreach( $info in $hotList1)
<a href="/bbsdoc.ejf?easyJWebCommand=show&&cid=$!info.cid" target="_blank">$!info.title</a><br>
    #end
   上面的脚本表示循环遍历hotList1集合中的对象,并输出对象的相关内容。
4、#macro(macroName)#end 脚本函数(宏)调用,不推荐在界面模板中大量使用。
   如:在使用EasyJWeb Tools快速生成的添删改查示例中,可以点击列表的标题栏进行升降排序显示,这是我们在EasyJWeb应用中经常看到的一个排序状态显示的模板内容。
   函数(宏)定义,一般放在最前面
   #macro(orderPic $type)
   #if ($orderField.equals($type))
   <img src="/images/ico/${orderType}.gif">
   #end
   #end
具体的调用如:<font color="#FFFFFF">头衔#orderPic("title")</font>

5、包含文件#inclue("模板文件名")或#parse("模板文件名")
  主要用于处理具有相同内容的页面,比如每个网站的顶部或尾部内容。
  使用方法,可以参考EasyJF开源Blog及EasyJF开源论坛中的应用!
  如:#parse("/blog/top.html")或#include("/blog/top.html")
  parse与include的区别在于,若包含的文件中有Velocity脚本标签,将会进一步解析,而include将原样显示。

6、#macro( tablerows $color $somelist )
#foreach( $something in $somelist )
    <tr><td bgcolor=$color>$something</td></tr>
#end
#end 
#macro相当于定义函数,其中tablerows为函数名,$color和$somelist相当与函数参数

小例子:
test.vm文件
<html>
<body>
#set($foo = "velocity")
Hello $foo world;
My name is $myname
Your name is $yourname
</body>
</html>

测试main:
import java.io.IOException;import java.io.StringWriter;import org.apache.velocity.Template;import org.apache.velocity.VelocityContext;import org.apache.velocity.app.VelocityEngine;import org.apache.velocity.exception.MethodInvocationException;import org.apache.velocity.exception.ParseErrorException;import org.apache.velocity.exception.ResourceNotFoundException;public class TestTemplateTheory {/** * @param args */public static void main(String[] args) {try {//初始化velocity引擎VelocityEngine ve = new VelocityEngine();ve.init();//獲取模板Template template = ve.getTemplate("test1.vm");//填充數據VelocityContext velocityContext = new VelocityContext();velocityContext.put("myname", "姚明");velocityContext.put("yourname", "麦迪");//合并數據StringWriter writer = new StringWriter();template.merge( velocityContext, writer );//顯示System.out.println(writer.toString());} catch (ResourceNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ParseErrorException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (MethodInvocationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


0 0
原创粉丝点击