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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM