首页编程velocity注释,jquery怎么使用velocity

velocity注释,jquery怎么使用velocity

编程之家2023-11-0793次浏览

这篇文章给大家聊聊关于velocity注释,以及jquery怎么使用velocity对应的知识点,希望对各位有所帮助,不要忘了收藏本站哦。

velocity注释,jquery怎么使用velocity

VelocityEngine 是什么包

org.apache.velocity.app.Velocity.类是一个初始化单例的velocity实例的类。

单例模式是一种简单的设计模式,用最简单的话来说,就是让多个类共享一个实例,让他们能够访问到同样的数据。

对比发现:

Velocity类中使用init方法里是使用了RuntimeSingleton.init();

在RuntimeSingleton类中,使用到的运行实例是静态new出来的。

private static RuntimeInstance ri= new RuntimeInstance();

velocity注释,jquery怎么使用velocity

而在org.apache.velocity.app.VelocityEngine类中,并没有一个单例类来初始化,每次都会使用

private RuntimeInstance ri= new RuntimeInstance()

来创建一个新的运行实例,这样就使得每个velocity引擎都会拥有各自的实例,互不干扰。

从RuntimeSingleton类的注释来看:

/**

* This is the Runtime system for Velocity. It is the

velocity注释,jquery怎么使用velocity

* single access point for all functionality in Velocity.

* It adheres to the mediator pattern and is the only

* structure that developers need to be familiar with

* in order to get Velocity to perform.

*

* The Runtime will also cooperate with external

* systems like Turbine. Runtime properties can

* set and then the Runtime is initialized.

*

* Turbine for example knows where the templates

* are to be loaded from, and where the velocity

* log file should be placed.

*/

velocity和外部合作的项目,例如turbine,就使用的这个类来实例化velocity引擎。

jquery怎么使用velocity

Velocity的基本用法<?xml:namespace prefix= o ns="urn:schemas-microsoft-com:office:office"/>

Velocity概述

Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。当Velocity应用于web开发时,界面设计人员可以和java程序开发人员同步开发一个遵循MVC架构的web站点,也就是说,页面设计人员可以只关注页面的显示效果,而由java程序开发人员关注业务逻辑编码。Velocity将java代码从web页面中分离出来,这样为web站点的长期维护提供了便利,同时也为我们在JSP和PHP之外又提供了一种可选的方案。

Velocity的能力远不止web站点开发这个领域,例如,它可以从模板(template)产生SQL和PostScript、XML,它也可以被当作一个独立工具来产生源代码和报告,或者作为其他系统的集成组件使用。 Velocity也可以为Turbine web开发架构提供模板服务(template service)。Velocity+Turbine提供一个模板服务的方式允许一个web应用以一个真正的MVC模型进行开发。

一、基本语法

1、"#"用来标识Velocity的脚本语句,包括#set、#if、#else、#end、#foreach、#end、#include、#parse、#macro等;

如:

#if($info.imgs)

<img src="$info.imgs" border=0>

#else

<img src="noPhoto.jpg">

#end

2、"$"用来标识一个对象(或理解为变量);如:$i、$msg、$TagUtil.options(...)等。

3、"{}"用来明确标识Velocity变量;

比如在页面中,页面中有一个$someonename,此时,Velocity将把someonename作为变量名,若我们程序是想在someone这个变量的后面紧接着显示name字符,则上面的标签应该改成${someone}name。

4、"!"用来强制把不存在的变量显示为空白。

如当页面中包含$msg,如果msg对象有值,将显示msg的值,如果不存在msg对象同,则在页面中将显示$msg字符。这是我们不希望的,为了把不存在的变量或变量值为null的对象显示为空白,则只需要在变量名前加一个“!”号即可。

如:$!msg

二、在EasyJWeb中的最佳实践

理论上你可以在EasyjWeb模板使用所有Velocity的脚本及功能,但我们不推荐你在界面模板中使用过多过复杂的脚本表达方式,在万不得已的情况下,不要在界面模板中加入任何复杂的逻辑,更不要在界面模板中加入变量声明、逻辑运算符等等。

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 rel="external nofollow" href="/bbsdoc.ejf?easyJWebCommand=show&&cid=$!info.cid"

target="_blank">$!info.title</a><br>

#end

上面的脚本表示循环遍历hotList1集合中的对象,并输出对象的相关内容。

4、#macro(macroName)#end脚本函数(宏)调用,不推荐在界面模板中大量使用。 Velocity中的宏我们可以理解为函数。①宏的定义#macro(宏的名称$参数1$参数2…)语句体(即函数体)#end

②宏的调用#宏的名称($参数1$参数2…)

说明:参数之间用空格隔开。

如:在使用EasyJWeb Tools快速生成的添删改查示例中,可以点击列表的标题栏进行升降排序显示,这是我们在EasyJWeb应用中经常看到的一个排序状态显示的模板内容。

函数(宏)定义,一般放在最前面

#macro(orderPic$type)

#if($orderField.equals($type))

<img src="http://images.cnblogs.com/ico/${orderType}.gif">

#end

#end

具体的调用如:<font color="#FFFFFF">头衔#orderPic("title")</font>

5、包含文件#inclue("模板文件名")或#parse("模板文件名")

主要用于处理具有相同内容的页面,比如每个网站的顶部或尾部内容。

如:#parse("/blog/top.html")或#include("/blog/top.html")

区别:

1若包含的文件中有Velocity脚本标签,将会进一步解析,而include将原样

显示。

2#parse只能指定单个对象。而#include可以有多个

示范代码:

#include("one.gif","two.txt","three.htm")

#parse("parsefoo.vm")

三、关于#set的使用

在万不得已的时候,不要在页面视图自己声明Velocity脚本变量,也就是尽量少使用#set。有时候我们需要在页面中显示序号,而程序对象中又没有包含这个序号属性同,可以自己定义。如在一个循环体系中,如下所示:

#set($i=0)

#foreach($info in$list)

序号:$i

#set($i=$i+1)

#end

四、Velocity脚本语法摘要

声明:#set($var=XXX)

变量引用,字面字符串,属性引用,方法引用,字面数字,数组列表。

#set($monkey=$bill)## variable reference

#set($monkey.Friend="monica")## string

#set($monkey.Blame=$whitehouse.Leak)## property reference

#set($monkey.Plan=$spindoctor.weave($web))## method reference

#set($monkey.Number= 123)##number

#set($monkey.Say= ["Not",$my,"fault"])## ArrayList

算术运算符

#set($foo=$bar+ 3)#set($foo=$bar- 4)#set($foo=$bar* 6)#set($foo=$bar/ 2)

2、注释:

单行## XXX

多行#* xxx

xxxx

xxxxxxxxxxxx*#

References引用的类型

3、变量 Variables

以"$"开头,第一个字符必须为字母。character followed by a VTL Identifier.(a.. z or A.. Z).

变量可以包含的字符有以下内容:

alphabetic(a.. z, A.. Z)

numeric(0.. 9)

hyphen("-")

underscore("_")

4、Properties

$Identifier.Identifier

$user.name

hashtable user中的的name值.类似:user.get("name")

5、Methods

object user.getName()=$user.getName()

6、Formal Reference Notation

用{}把变量名跟字符串分开

#set($user="csy"}

${user}name

返回csyname

$username

$!username

$与$!的区别

当找不到username的时候,$username返回字符串"$username",而$!username返回空字符串""

7、双引号与引号

#set($var="helo")

test"$var"返回testhello

test'$var'返回test'$var'

可以通过设置 stringliterals.interpolate=false改变默认处理方式

8、条件语句

#if($foo)

<strong>Velocity!</strong>

#end

#if($foo)

#elseif()

#else

#end

当$foo为null或为Boolean对象的false值执行.

9、逻辑运算符:==&&||!

10、循环语句#foreach($var in$arrays)//集合包含下面三种Vector, a Hashtable or an Array

#end

#foreach($product in$allProducts)

<li>$product</li>

#end

#foreach($key in$allProducts.keySet())

<li>Key:$key-> Value:$allProducts.get($key)</li>

#end

#foreach($customer in$customerList)

<tr><td>$velocityCount</td><td>$customer.Name</td></tr>

#end

语句的嵌套#foreach($element in$list)## inner foreach内循环#foreach($element in$list) This is$element.$velocityCount<br>inner<br>#end## inner foreach内循环结束## outer foreach This is$element.$velocityCount<br>outer<br>#end

11、velocityCount变量在配置文件中定义

# 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

12、包含文件

#include("one.gif","two.txt","three.htm")

13、Parse导入脚本

#parse("me.vm")

14、#stop停止执行并返回

停止执行模板引擎并返回,把它应用于debug是很有帮助的。

15、定义宏Velocimacros,相当于函数支持包含功能

#macro( d)

<tr><td></td></tr>

#end

调用

#d()

16、带参数的宏

#macro( tablerows$color$somelist)

#foreach($something in$somelist)

<tr><td bgcolor=$color>$something</td></tr>

#end

#end

17、Range Operator

#foreach($foo in [1..5])

18、转义字符

如果reference被定义,两个’\’意味着输出一个’\’,如果未被定义,刚按原样输出。#set($email="foo")$email\$email\\$email\\\$email

输出: foo$email....

非常感谢您的阅读!我们希望本文对于解决您关于velocity注释的问题提供了一些有价值的信息。如果您还有其他疑问,我们将很乐意为您提供进一步的帮助。

choose函数?怎么在excel中使用CHOOSE函数ip查询地址(ip地址怎么查 三种方法教你轻松查询)