`
sillycat
  • 浏览: 2552168 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

velocity笔记

    博客分类:
  • UI
阅读更多
velocity笔记
年青的时候使用velocity的笔记,真是还念那段日子。
其实就是吧demo里面的文章通读了后的摘抄而已,每次使用velocity前先温习一下,然后再去使用。

<HTML>
<BODY>
Hello $customer.Name!
<table>
#foreach( $mud in $mudsOnSpecial )
   #if ( $customer.hasPurchased($mud) )
      <tr>
        <td>
          $flogger.getPromo( $mud )
        </td>
      </tr>
   #end
#end
</table>

1 define a variable: a
#set( $a = "Velocity" )
String values are always enclosed in quotes, either single or double quotes.
Single quotes will ensure that the quoted value will be assigned to the reference as is.
Double quotes allow you to use velocity references and directives to interpolate, such as "Hello $name",

where the $name will be replaced by the current value before that string literal is assigned to the left

hand side of the =

References begin with $ and are used to get something. Directives begin with # and are used to do

something.

#set( $size = "Big" )
      #set( $name = "Ben" )

      #set($clock = "${size}Tall$name" )

      The clock is $clock.
Now the output is 'The clock is BigTallBen'.

2 Comments
A single line comment begins with ## and finishes at the end of the line. If you're going to write a few

lines of commentary, there's no need to have numerous single line comments. Multi-line comments, which

begin with #* and end with *#, are available to handle this scenario.
There is a third type of comment, the VTL comment block, which may be used to store such information as

the document author and versioning information: #** third comment type*#
3 References
Everything coming to and from a reference is treated as a String object. If there is an object that

represents $foo (such as an Integer object), then Velocity will call its .toString() method to resolve

the object into a String.
Take the first example, $customer.Address. It can have two meanings. It can mean, Look in the hashtable

identified as customer and return the value associated with the key Address. But $customer.Address can

also be referring to a method (references that refer to methods will be discussed in the next section);

$customer.Address could be an abbreviated way of writing $customer.getAddress(). When your page is

requested, Velocity will determine which of these two possibilities makes sense, and then return the

appropriate value.
4 Method
$page.setTitle( "My Home Page" )
$person.setAttributes( ["Strange", "Weird", "Excited"] )
How to user {} to protect the $charater
Jack is a pyromaniac
Jack is a kleptomaniac
Jack is a ${vice}maniac

<input type="text" name="email" value="$email"/>
<input type="text" name="email" value="$!email"/>
<input type="text" name="email" value="$!{email}"/>

5 Getting Literal

Currency
There is no problem writing "I bought a 4 lb. sack of potatoes at the farmer's market for only $2.50!"

As mentioned, a VTL identifier always begins with an upper- or lowercase letter, so $2.50 would not be

mistaken for a reference
Escaping Valid VTL References
#set( $email = "foo" )
$email                        
\$email
\\$email
\\\$email

foo
$email
\foo
\$email

#set( $foo = "gibbous" )
$moon = $foo
The output is $moon = "gibbous"

6 Case Substitution
$foo
$foo.getBar()
## is the same as
$foo.Bar

$data.setUser("jon")
## is the same as
#set( $data.User = "jon" )

When the method getFoo() is referred to in a template by $bar.foo, Velocity will first try $getfoo. If

this fails, it will then try $getFoo. Similarly, when a template refers to $bar.Foo, Velocity will try

$getFoo() first and then try getfoo().

7 Directive
#if($a==1)true enough#elseno way!#end
#if($a==1)true enough#{else}no way!#end

#set( $monkey.Say = ["Not", $my, "fault"] ) ## ArrayList
For the ArrayList example the elements defined with the [..] operator are accessible using the

methods defined in the ArrayList class. So, for example, you could access the first element above using

$monkey.Say.get(0).
#set( $monkey.Map = {"banana" : "good", "roast beef" : "bad"}) ## Map
   for the Map example, the elements defined within the { } operator are accessible using

the methods defined in the Map class. So, for example, you could access the first element above using

$monkey.Map.get("bannana") to return a String 'good', or even $monkey.Map.banana to return the same

value.

#set( $value = $foo + 1 )
#set( $value = $bar - 1 )
#set( $value = $foo * $bar )
#set( $value = $foo / $bar )
The RHS can also be a simple arithmetic expression:

Velocity has logical AND, OR and NOT operators as well. For further information
## logical AND

#if( $foo && $bar )
   <strong> This AND that</strong>
#end
## logical OR

#if( $foo || $bar )
    <strong>This OR That</strong>
#end
##logical NOT

#if( !$foo )
<strong>NOT that</strong>
#end

8 ForEach
<table>
#foreach( $customer in $customerList )
    <tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
</table>
The default name for the loop counter variable reference, which is specified in the velocity.properties

file, is $velocityCount. By default the counter starts at 1, but this can be set to either 0 or 1 in the

velocity.properties file. Here's what the loop counter properties section of the velocity.properties

file appears:
# Default name of the loop counter
# variable reference.
directive.foreach.counter.name = velocityCount

# Default starting value of the loop
# counter variable reference.
directive.foreach.counter.initial.value = 1

9 #Include
The contents of the file are not rendered through the template engine. For security reasons, the file to

be included may only be under TEMPLATE_ROOT.
#include( "one.txt" )
The file to which the #include directive refers is enclosed in quotes. If more than one file will be

included, they should be separated by commas.
#include( "one.gif","two.txt","three.htm" )

10 Parse
The #parse script element allows the template designer to import a local file that contains VTL.

Velocity will parse the VTL and render the template specified.
#parse( "me.vm" )
Any templates to which #parse refers must be included under TEMPLATE_ROOT. Unlike the #include

directive, #parse will only take a single argument.

11 #stop
The #stop script element allows the template designer to stop the execution of the template engine and

return. This is useful for debugging purposes.

12 #macro
#macro( tablerows $color $somelist )
#foreach( $something in $somelist )
       <tr><td bgcolor=$color>$something</td></tr>
#end
#end
#set( $greatlakes = ["Superior","Michigan","Huron","Erie","Ontario"] )
#set( $color = "blue" )
<table>
   #tablerows( $color $greatlakes )
</table>
The OutPut:
<table>
    <tr><td bgcolor="blue">Superior</td></tr>
    <tr><td bgcolor="blue">Michigan</td></tr>
    <tr><td bgcolor="blue">Huron</td></tr>
    <tr><td bgcolor="blue">Erie</td></tr>
    <tr><td bgcolor="blue">Ontario</td></tr>
</table>

#macro( inner $foo )
inner : $foo
#end

#macro( outer $foo )
   #set($bar = "outerlala")
   outer : $foo
#end

#set($bar = 'calltimelala')
#outer( "#inner($bar)" )

The output is :
Outer : inner : outerlala
What is Velocimacro Autoreloading?
There is a property, meant to be used in development, not production :
velocimacro.library.autoreload
which defaults to false. When set to true along with
<type>.resource.loader.cache = false
file.resource.loader.path = templates
    file.resource.loader.cache = false
    velocimacro.library.autoreload = true

13 Range Operater
First example:
#foreach( $foo in [1..5] )
$foo
#end

Second example:
#foreach( $bar in [2..-2] )
$bar
#end

Third example:
#set( $arr = [0..1] )
#foreach( $i in $arr )
$i
#end

Fourth example:
[1..3]

First example:
1 2 3 4 5

Second example:
2 1 0 -1 -2

Third example:
0 1

Fourth example:
[1..3]
Both n and m must either be or produce integers. Whether m is greater than or less than n will not

matter; in this case the range will simply count down. Examples showing the use of the range operator as

provided below:



分享到:
评论

相关推荐

    Velocity语法笔记

    ### Velocity 语法笔记 #### 一、Velocity 概述与基本用法 Velocity 是一个基于 Java 的模板引擎,主要用于 Web 应用程序中生成动态页面。它提供了丰富的语法支持,使得开发者可以更轻松地处理数据并将其转换为...

    velocity学习笔记

    ### Velocity学习笔记精要 **一、Velocity简介与特点** Velocity是一种基于Java的模板引擎,用于将静态数据和动态内容结合在一起,生成最终的HTML、XML或其他格式的文档。其最大的特点是性能高、易于理解和使用,...

    velocity学习笔记与struts2整合

    Velocity是Apache软件基金会的一个开源项目,它是一款快速、强大且易用的模板引擎,用于生成动态Web内容。在Java世界中,Velocity常被用来作为MVC框架中的视图层技术,与Struts2等框架集成,以实现更灵活的页面渲染...

    崔希凡javaweb28天笔记

    7. **模板引擎**:如FreeMarker或Velocity的使用,用于动态生成HTML页面。 8. **异常处理**:在Java Web中的全局异常处理机制,以及如何自定义异常类。 9. **安全问题**:如防止SQL注入、XSS攻击和CSRF攻击的策略...

    狂神说redis笔记

    大数据时代的特征可以概括为“3V+3高”,即海量的数据量(Volume)、多样化的数据类型(Variety)、快速的数据流(Velocity),以及高并发、高可扩展性和高性能等要求。 通过狂神的Redis笔记,我们可以系统地学习...

    张龙圣思园struts2学习笔记word

    在实际开发中,Struts2支持多种视图技术,如JSP、FreeMarker、Velocity等,使得开发者可以根据项目需求选择最适合的模板语言。Action结果类型也是多样化的,可以返回字符串、Stream、甚至重定向或转发到其他页面。 ...

    狂神说springmvc笔记.zip

    如Velocity或Freemarker作为模板引擎,可以生成动态HTML内容,实现前后端分离。 在"狂神说springmvc笔记.zip"中,读者可以通过深入学习这些知识点,掌握SpringMVC的使用技巧和最佳实践,提升自己的Java Web开发...

    struts2 学习重点笔记

    - **视图**:负责向用户展示界面,通常使用 JSP 或模板技术如 FreeMarker 和 Velocity。 - **控制器**:处理用户的请求,协调模型和视图,使用 Servlet、Action 实现。 **1.3 Struts2 的特性** - **单点控制**:...

    webworkwebwork笔记

    从给定的文件信息来看,这里涉及到的是WebWork框架的学习笔记与配置,以及如何将WebWork与Spring、Hibernate集成在一起的示例。下面,我们将详细地解析这些知识点: ### WebWork框架简介 WebWork是一个开源的Java ...

    大数据导论学习记录笔记

    * 大数据的5个特点:volume(规模大)、variety(类型多)、velocity(速度快)、value(有价值)、veracity(真实性) 大数据技术属性 * 云计算:一种无处不在的、便捷的且按需的对一个共享的可配置的计算资源...

    狂神说Redis笔记.pdf

    在大数据时代,Nosql通常需要应对3V问题(Volume、Variety、Velocity)以及3高的要求(High concurrency、High availability、High performance),因此Nosql数据库在设计时会特别考虑这些问题。 狂神的Redis笔记不...

    bigdata笔记1

    "bigdata笔记1"可能包含的是对大数据基础知识、主要技术框架及其应用的概述。以下是一些可能涵盖的重要知识点: 1. **大数据定义**:大数据不仅仅是数据的量大,它还包括数据的多样性、速度和价值。大数据的4V特性...

    大数据技术原理学习笔记.docx

    本笔记基于林子雨老师在MOOC上的《大数据技术原理》课程,旨在为IT从业者和大学生提供一个全面了解大数据的基础框架。 首先,我们要认识到大数据的发展背景。随着互联网的普及,以及物联网、社交媒体、移动设备等...

    struts2综合笔记

    - 支持多种视图技术,如 JSP、FreeMarker、Velocity 等。 - 基于 Spring AOP 思想的拦截器机制,易于扩展和定制。 - 强大的输入校验功能。 **历史背景**: - **Struts1 vs. Struts2**: - **共同点**: 都遵循 MVC...

Global site tag (gtag.js) - Google Analytics