Velocityѧϰ

匿名 (未验证) 提交于 2019-12-03 00:17:01
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11790482.html

Velocityѧϰ:

1. velocity对大小写敏感  2. velocity的具体用法:          import org.apache.velocity.Template;     import org.apache.velocity.VelocityContext;     import org.apache.velocity.app.VelocityEngine;     import org.apache.velocity.runtime.RuntimeConstants;     import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;      /**      * 初始化Velocity引擎      */     VelocityEngine ve = new VelocityEngine();     ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");     ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());      ve.init();      /**      * 获取模板,参数栏填写模板所在路径      */     Template t = ve.getTemplate("ureport-html/html-preview.html");      /**      * 创建Velocity上下文      */     VelocityContext vctx = new VelocityContext();     /**      * 放入数据      */     vctx.put("data", "data");     vctx.put("html", "htmldata");      /**      * 创建一个任意形式的wirter,用于输出页面数据      * 假设前面有response传入      */     response.setContentType("text/html");     response.setCharacterEncoding("utf-8");     //Writer writer = new Writer(); //可以new一个Writer, 也可以从response中获取,设置输出的信息     PrintWriter writer = response.getWriter();      /**      * 交给模板去处理      */     t.merge(vctx, writer)      writer.close();  3. 语法      #set( $foo = "Velocity" ) 设置变量的值      ## 单行注释      #*  随便什么内容     *# 多行注释      $user 引用Java中定义的对象      $user.name 引用user对象的属性      $user.setName("fw"); 方法引用      $user.getName(); 这个和$user.name 效果是一样的      如果有一个对象是引用的Java数组,那还可以直接调用Java数组的方法:          $myarray.isEmpty()         $myarray.size()         $myarray.get(2)         $myarray.set(1, 'test')      ${userName} 这种写法也支持      $!userName 如果userName是空值, 那么就显示""空字符串      ${!userName} ${}是正式引用; !是静态引用      不解析模式:下面的代码将会输出 #[[ ]]#中的内容;     #[[         #foreach ($item in $itemList)           nothing will happen to $item         #end     ]]#      判断语句 If / ElseIf / Else  && || !        #if ()         //do something     #elseif ()         //do something     #elseif ()         //do something     #else          //do something     #end      循环 foreach      #foreach ($name in $users)         $name     #end      #include( "one.png","two.txt","three.html" )      #parse( "me.vm" )

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!