Velocity中的ComparisonDateTool、MathTool、NumberTool、SortTool、EscapeTool工具

可以从Apache官方网站自带的例子中查找,位置\velocity-tools-1.4-src\examples\showcase。

Apache官方网站自带的例子有下面一些:

使用Velocity Tools的时候,一定要在toolbox.xml中指定,然后在应用的web.xml中配置。

这里,关于jar包的引用,很可能出现一种Velocity运行时异常,例如:

信息: Velocity   [warn] org.apache.velocity.runtime.exception.ReferenceException: reference : template = /templates/context.vm [line 12,column 37] : $context is not a valid reference.
2008-4-23 9:41:45 org.apache.catalina.core.ApplicationContext log

息: Velocity   [warn]
org.apache.velocity.runtime.exception.ReferenceException: reference :
template = /templates/context.vm [line 16,column 37] : $context.keys is
not a valid reference.
2008-4-23 9:41:45 org.apache.catalina.core.ApplicationContext log

息: Velocity   [warn]
org.apache.velocity.runtime.exception.ReferenceException: reference :
template = /templates/context.vm [line 20,column 37] : $context.toolbox
is not a valid reference.
2008-4-23 9:41:45 org.apache.catalina.core.ApplicationContext log

息: Velocity   [warn]
org.apache.velocity.runtime.exception.ReferenceException: reference :
template = /templates/context.vm [line 24,column 37] : $context.values
is not a valid reference.

关键就是jar包的选择有问题,还可能是没有加入指定的工具的jar包。

选择几个工具:

org.apache.velocity.tools.generic.ComparisonDateTool

在toolbox.xml中配置如下所示:

<tool>
    <key>date</key>
    <scope>application</scope>
    <class>org.apache.velocity.tools.generic.ComparisonDateTool</class>
    <parameter name="format" value="yyyy-MM-dd"/>
    <parameter name="depth" value="2"/>
    <parameter name="skip" value="month"/>
</tool>

date就像一个内置对象,你随时可以在.vm的Velocity模板中使用。

关于date,如下所示:

函数举例 显示结果
$date 2008-04-22
$date.long 2008年4月22日 下午10时58分54秒
$date.medium_time 22:58:54
$date.full_date 2008年4月22日 星期二
$date.yyyy 2008
$date.get('default','short') 2008-4-22 下午10:58
$date.get('yyyy-M-d H:m:s') 2008-4-22 22:58:54
$date.whenIs('2008-04-20') 2 days 22 hours ago
$date.whenIs('2008-04-20').full 2 days 22 hours 58 minutes 54 seconds 484 milliseconds ago
$date.whenIs('2008-04-20').days -2
$date.whenIs('2008-04-20') 22 hours 58 minutes ago
$date.whenIs('2008-04-20','2008-04-20') same time
$date.difference('2008-04-20', '2008-04-20') 2 days
$date.whenIs('2008-04-20').days -2

org.apache.velocity.tools.generic.MathTool

在toolbox.xml中配置如下所示:

<tool>
    <key>math</key>
    <scope>application</scope>
    <class>org.apache.velocity.tools.generic.MathTool</class>
</tool>

关于math,如下所示:

函数举例 显示结果
$math.add("10","4") 14
$math.sub("100","20") 80
$math.mul("11","7") 77
$math.div("100","5") 20
$math.idiv("100","5") 20
$math.pow("2","5") 32
$math.mod("13","5") 3
$math.max("99","16") 99
$math.min("99","16") 16
$math.abs("-99") 99
\$math.cell("99.26") $math.cell("99.26")
$math.floor("99.26") 99
$math.random 0.7663665545444911
$math.random("1","10") 2
$math.roundTo("2","5") 5.0
$math.toInteger("15") 15
$math.toDouble("15") 15.0
$math.toNumber("15") 15

org.apache.velocity.tools.generic.NumberTool

在toolbox.xml中配置如下所示:

<tool>
    <key>number</key>
    <scope>application</scope>
    <class>org.apache.velocity.tools.generic.NumberTool</class>
    <parameter name="format" value="#0.0"/>
</tool>

关于number,如下所示:

函数举例 显示结果
$number org.apache.velocity.tools.generic.NumberTool@32c41c
$number.currency("35") ¥35.00
$number.format #0.0
$number.locale zh_CN
$number.format("35") 35.0
$number.integer("35.47") 35
$number.number("35.47") 35.47
$number.percent("0.3547") 35%
$number.toNumber("35.47") 35.47

org.apache.velocity.tools.generic.SortTool

在toolbox.xml中配置如下所示:

<tool>
    <key>sorter</key>
    <scope>application</scope>
    <class>org.apache.velocity.tools.generic.SortTool</class>
</tool>

关于sorter,如下所示:

函数举例 显示结果
$sorter.sort(['d','a','b','c']) [a, b, c, d]
$sorter.sort([5,1,12,3]) [1, 3, 5, 12]

sorter工具,可以对指定的集合进行排序,如List,Array,Map等等。

org.apache.velocity.tools.generic.EscapeTool

在toolbox.xml中配置如下所示:

<tool>
    <key>esc</key>
    <scope>application</scope>
    <class>org.apache.velocity.tools.generic.EscapeTool</class>
</tool>

关于esc,如下所示:

函数举例 显示结果
$esc org.apache.velocity.tools.generic.EscapeTool@e09a07
$esc.b \
$esc.backslash \
$esc.d $
$esc.dollar $
$esc.e !
$esc.exclamation !
$esc.h #
$esc.hash #
$esc.q "
$esc.quote "
\esc.s esc.s
$esc.singleQuote '
$esc.html("$esc.backslash") \
$esc.url("shirdrn.org/users/index.vm?id=1&name=异域王者") shirdrn.org%2Fusers%2Findex.vm%3Fid%3D1%26name%3D%E5%BC%82%E5%9F%9F%E7%8E%8B%E8%80%85
$esc.sql("SELECT * FROM PERSON WHERE ID='2008042319831'") SELECT * FROM PERSON WHERE ID=''2008042319831''
\$esc.java() [说明]Escapes the characters in a String using Java String rules.
\$esc.javascript() [说明]Escapes the characters in a String using JavaScript String rules.
\$esc.xml() [说明]Escapes the characters in a String using XML entities.
\$esc.propertyKey() [说明]Escapes the characters in a String using java.util.Properties rules for escaping keys.
\$esc.propertyValue() [说明]Escapes the characters in a String using java.util.Properties rules for escaping values.
 
来自

Velocity中的ComparisonDateTool、MathTool、NumberT...的更多相关文章

  1. 记录一次bug解决过程:velocity中获取url中的参数

    一.总结 在Webx的Velocity中获取url中参数:$rundata.getRequest().getParameter('userId') 在Webx项目中,防止CSRF攻击(Cross-si ...

  2. Velocity中避免null引起的数据问题

    请先看下面一段代码: #foreach($id in [1..50]) #set($user = $User.Get($id)) $id : ${user.name} #end 上面这段代码中,假设只 ...

  3. velocity中使用枚举

    版权声明:本文为博主原创文章,转载请注明出处,欢迎使劲喷 一.为什么要在velocity中使用枚举 1.目前接触到的系统,枚举通常用来在程序中定义数据字典. 举个支付的例子,比如一个字段用来标识一条记 ...

  4. 转 Velocity中加载vm文件的三种方式

    Velocity中加载vm文件的三种方式   velocitypropertiespath Velocity中加载vm文件的三种方式:    方式一:加载classpath目录下的vm文件 Prope ...

  5. Velocity中加载vm文件的三种方式

    Velocity中加载vm文件的三种方式: a.  加载classpath目录下的vm文件 /** * 初始化Velocity引擎 * --VelocityEngine是单例模式,线程安全 * @th ...

  6. Velocity中判断表达式是不是为空

    Velocity中判断表达式是不是为空 $if ($null.isNull($mycoll) || $mycoll.size()==0) ${}与$!{}区别 例子: ${str}:如果str没有值, ...

  7. velocity中$springMacroRequestContext.getMessage($code)

    在Java国际化(i18n)中, vm页面显示内容需要使用 #springMessage("title") 实际运行时发现页面输出$springMacroRequestContex ...

  8. Velocity中#set指令

    引用可以让模板设计者生成动态内容,而指令允许设计者真正的负责页面的展现和内容. 指令是以#开头,后面紧跟一个关键字,比如if,else,foreach等.而这个关键字,是可以被放在{}中间的.这是规范 ...

  9. velocity中加载模板文件的方式

    velocity有多中种方式供我们去加载我们自定义的模板文件,下面详细的介绍使用的方法. 1.1.1. 加载classpath目录下的模板文件 使用classpath方式加载,是我们经常用到的一种方式 ...

随机推荐

  1. CCF CSP 201612-2 工资计算

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201612-2 工资计算 问题描述 小明的公司每个月给小明发工资,而小明拿到的工资为交完个人所 ...

  2. python二叉树简单实现

    二叉树简单实现: class Node: def __init__(self,item): self.item = item self.child1 = None self.child2 = None ...

  3. hdoj1232 畅通工程(并查集)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1232 思路 使用并查集求解. 代码 #include <iostream> #includ ...

  4. TP5:使用了INPUT函数来接收参数了,还需再过滤SQL注入吗

    TP5:使用了INPUT函数来接收参数了,还需再过滤SQL注入吗,默认的INPUT函数都做了哪些动作啊 有了PDO参数绑定 基本上不需要考虑sql注入的问题(除非自己拼接SQL),需要考虑的是XSS方 ...

  5. python脚本获取本机公网ip

    1.获取公网IP地址方式,访问:http://txt.go.sohu.com/ip/soip 2.python脚本实现: #!/usr/bin/python # -*- coding:utf8 -*- ...

  6. [CodeForces - 848B] Rooter's Song 思维 找规律

    大致题意: 有一个W*H的长方形,有n个人,分别站在X轴或Y轴,并沿直线向对面走,第i个人在ti的时刻出发,如果第i个人与第j个人相撞了 那么则交换两个人的运动方向,直到走到长方形边界停止,问最后每个 ...

  7. 超实用 Git 使用方式介绍

    都说程序员若是不知道 GitHub 就不是一个合格的程序员,其实这话说的过分了,不知道就学嘛,今天我们就来说说 Git 和 GitHub 到底是什么. 我们在开发软件的时候,常常是需要多人协作完成,这 ...

  8. 将已有的项目提交到GitHub

    1.目的: 将已有的项目提交到GitHub 2.准备工作 2.1 此教程建立在对git有初步的理解上 2.2 此教程之前需准备工作 a.熟悉git的一些基本命令和原理. b.已注册有GitHub账号. ...

  9. iOS 11开发教程(九)iOS11数据线连接真机测试

    iOS 11开发教程(九)iOS11数据线连接真机测试 在Xcode 7.0之后,苹果公司在开发许可权限上做了很多的改变,在测试App方面取消了一些限制.在Xcode7.0之前的版本,苹果公司只向注册 ...

  10. github下载项目