我有重複元素的列表中刪除從列表中重複的元素,我需要使用速度如何使用Apache速度
例如,帖子中包含重複的元素
#foreach ($p in $posts)
$p.name //will be unique
#end
我想刪除重複使用速度,
任何幫助,將不勝感激
我有重複元素的列表中刪除從列表中重複的元素,我需要使用速度如何使用Apache速度
例如,帖子中包含重複的元素
#foreach ($p in $posts)
$p.name //will be unique
#end
我想刪除重複使用速度,
任何幫助,將不勝感激
你不能做到這一點的速度。你必須提供一個不包含重複的模型。最簡單的方法是使用new HashSet<Post>(postsList)
- 這將消除重複(基於equals(..)
法)
如果你真的無法通過適當的模型,你可以嘗試定義一個custom tool是獲得一個列表,並返回一組,但這並不容易。
除了速度不可能,從架構的角度來看,你想要什麼都沒有意義。 「刪除重複」部分是某種邏輯,需要在適當的地方處理。一個觀點並不適合這樣做。所以你應該盡一切辦法在Java中做到這一點,甚至在Velocity中這是不可能的。
即使您的角色不允許更改Java代碼,仍必須使用Java解決此問題。
只是爲了爭辯,因爲其他人說這是不可能與速度,我想表明它實際上可能與速度,但仍然不建議。
對於那些有興趣誰如何可以做到:
#set($uniquePosts = [])
#foreach($post in $posts)
#set($exists = false)
#foreach($uniquePost in $uniquePosts)
#if($uniquePost.name == $post.name)
#set($exists = true)
#break
#end
#end
#if(!$exists)
#set($added = $uniquePosts.add($post))
#end
#set($posts = $uniquePosts)
#end
Unique list:
#foreach($post in $posts)
$post.name
#end
這是可能的,這應該取決於你的速度的版本。比上面的答案更簡潔一點。
#set($uniquePosts = [])
#foreach($post in $posts)
#if(! $uniquePosts.contains($post.name) )
#if($uniquePosts.add($post.name)) #end
##note the if above is to trap a "true" return - may not be required
$post.name
#end
#end
這樣的事情應該在java方面解決,Velocity沒有設計用來構造數據結構。 – serg