抑制已经遇到的列表元素 [英] suppress list elements that have already been encountered
问题描述
我有一个正在尝试编辑的速度模板
Hi I have a velocity template that I am trying to edit
它目前有一个看起来像的块
it currently has a block that looks like
#foreach( $element in $myList )
$element.field1 ($element.field2) issued by $element.field ($element.field4 )
<br><br>
#end
问题是列表中的某些元素是重复的,我需要抑制重复.
the problem is some elements in the list are duplicated and I need to suppress the duplicates.
我想要的伪代码是
for each element in list
if element is not in displayed
display element
add element to displayed
endif
endfor
有人能指出我正确的方向吗?
can someone point me in the right direction?
推荐答案
这种逻辑(去重)可能是你的视图(Velocity)层要避免的.按照 Model-View-Controller,它会最好让控制器类管理这个逻辑,让 Velocity 模板简单地呈现它传递的数据结构.
This kind of logic (de-duplication) is probably something to be avoided in your view (Velocity) layer. Following Model-View-Controller, it would be better to have this logic managed by controller classes, leaving the Velocity template to simply render the data structure it is passed.
例如,通过使用诸如 java.util.Set
之类的数据结构,将不允许重复,因此模板不需要去重复.
For example, by using a data structure such as a java.util.Set
, duplicates would not be admitted and so there would be no need for the template to de-duplicate.
我个人发现 Rob Harrop 的 Pro Jakarta Velocity 非常好的 MVC 指南,尤其是第 4 章使用MVC 环境中的速度".
Personally I found Rob Harrop's Pro Jakarta Velocity a really good guide to MVC, especially Chapter 4 "Using Velocity in an MVC environment".
让模型使用一个集合,让你的控制器代码填充这个集合,然后模板代码中的简单循环就可以像现在一样使用了.
Have the model use a Set, have your controller code populate the set, and then the simple loop in your template code can be used as it is now.
通常,您在视图层中实现的逻辑越少越好.它还将使您的代码更易于测试,因此您无需启动演示组件、应用服务器等即可验证其行为.
In general, the less logic you implement in your view layer, the better. It will also make your code easier to test, so you can verify its behaviour without having to fire up the presentation components, app servers etc etc.
如果实在没有选择,逻辑绝对必须写在模板中,那么下面实现了呈现的伪代码:
If there really is no choice and the logic absolutely must be written in the template, then the following implements the psuedocode presented:
#set($displayed = [])
#foreach( $element in $myList )
#if(!$displayed.contains($element))
$element.field1 ($element.field2) issued by $element.field ($element.field4 )
<br><br>
#set($ignore = $displayed.add($element))
#end
#end
注意 #set($ignore = $displayed.add($element))
的混乱 - 必须这样做以抑制 java.util.List
的输出code> 的 add()
方法(布尔值)不会被输出.另一个不在模板代码中写这个的原因!
Note the messiness with #set($ignore = $displayed.add($element))
- this has to be done to suppress the output from java.util.List
's add()
method (boolean) from being output. Another reason not to write this in template code!
当然,您还需要确保 equals()
在添加到列表的类型上正确实现,以便列表操作 - contains()
和 add()
正常工作.
Of course, you would also need to make sure that equals()
is correctly implemented on the type added to the list so that the List operations - contains()
and add()
work correctly.
绝对是上述 MVC 方法的劣质解决方案,但作为最后的选择提出.
Definitely an inferior solution to the MVC approach above, but presented as an option of last resort.
这篇关于抑制已经遇到的列表元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!