Velocity常用方法

1.字符串替换 replace
#if($!{name} != '')
#set($tempName = $!{name})
#set($tempName = $tempName.Replace('abc','def'))
$tempName
#end


2.decimal数据类型转换成一定的字符串格式 tostring
需要计算的:如 (number/1000).tostring("f1");
#if($strDecimal)
$strDecimal.ToString(""f0"")<br/>
#end <br/>

#if($objectDecimal)
$objectDecimal.Price.ToString(""f0"")<br/>
#end <br/>

#if($tableDecimal)
#foreach($model in $tableDecimal.Rows)
$model.Price.ToString(""f0"")<br/>
#end
#end


3.DateTime数据类型转换成一定的字符串格式 tostring
#if($datetime)
$datetime.ToString(""yyyy-MM-dd"")<br/>
#end <br/>

#if($date)
$date.time.ToString(""yyyy-MM-dd hh:mm:ss"")<br/>
#end <br/>

#if($table)
#foreach($model in $table.Rows)
$model.time.ToString(""yyyy年MM月dd日"")<br/>
#end
#end


4.Trim() 去除空格
#if($!{name} != '')
#set($tempName =$!{name})
#if($tempName == ' abc ')
还没有去除首尾空格<br/>
#end
#set($tempName =$tempName.Trim())
#if($tempName == 'abc')
去除成功
#end
$tempName
#end

5.获取对象条数
Datatable: $table.Rows.Count
数组: $tempList.Length
List: $tempList.Count
字符串长度 : str.Length

6.
判断是否为’’
#if($!{name} != ‘’)
有值
#end
#if($!{name} == ‘’)
空值
#end

7.
null、notnull的用法
#if($!{name})
非空
#end
#if(! $!{name})

#end

8.
SubString()的用法
#set($tempName = $!{name})
#if($tempName.Length > 70)
$tempName.Substring(0,70)...
#else
$tempName
#end

9.velocityCount 序值

10.获取数组元素(不能直接arr[]):$arr.get_item(0)

11.分割字符串

#set($str="111#222")
#set($arr=$UtilHelper.SpiltString("$str","#"))
<p>$arr.length</p>
#foreach($item in $arr)
<h2>$item</h2>
#end

posted @ 2015-01-30 11:07  RobertChan  阅读(6957)  评论(0编辑  收藏  举报