速度foreach排序列表 [英] Velocity foreach sort list
问题描述
到目前为止,我试过了
<$ p $在$ {pro.test} .sort($ {pro.test.sortOrder}))
...
#end
但是这是抛出错误,我还试着用
pre $ $ pro.test.sort($ { ...
#end
这一次没有错误,但是控制不是进入循环,
时调用其次, #foreach
非常不严格,当您尝试在迭代范围内传递无效的东西时,它不会抱怨;如果它不解析为一个有效的列表,那么它只是忽略它,认为没有什么可以迭代。当某些事情不能按预期工作时,打印出您正在使用的值:
$ pro.test $ pro.test .class $ pro.test.sortOrder $ pro.test.sortOrder.class
#foreach($ in $ {pro.test.sort($ {pro.test.sortOrder})})
。 ..
#end
这是什么打印?是 $ pro.test
确实是一个集合?是 $ pro.test.sortOrder
一个有效的排序顺序规范,正如所期望的 SortTool
?
如果 $ pro.test
是 设置
界面,那么没有 sort
或 sortOrder
方法。假设在 $ sorter
变量下可以使用Velocity SortTool
,对于初学者来说,可以尝试一下:
#foreach($ sorter.sort($ pro.test))
...
#end
现在, $ pro.test.sortOrder
实际上会尝试调用下面的一个: getsortOrder()
, getSortOrder()
, get(sortOrder)
, issortOrder / code>,
isSortOrder()
。
Can any one suggest how to sort this list in velocity ?
So far I tried
#foreach($it in ${pro.test}.sort(${pro.test.sortOrder}))
.........
#end
But it is throwing error, Also I tried with
#foreach($it in ${pro.test.sort(${pro.test.sortOrder})})
.........
#end
this time no error but the control is not coming inside loop,
First, using ${...}
is a way of helping the Velocity parser know exactly what you consider to be Velocity code. Normally, when velocity sees $something.somethingElse.somethingMore
, it tries to parse as much as it can until it sees a word breaking character, such as a space or comma. When it sees ${something.somethingElse}.somethingMore
, it only reads until the matching }
as actual code, the rest is plain text. This means that ${pro.test}.sort(...)
considers .sort(...)
as plain text that should be printed, so it wouldn't call it as a method. That's why the first example fails with an error. You only need to use the formal syntax outside directives, though, you can just remove all the {
and }
from the call when inside #foreach(...)
.
Second, #foreach
is very lax, it doesn't complain when you try to pass something invalid in the iterated scope; if it doesn't resolve to a valid list, then it simply ignores it and considers that there's nothing to iterate over. When something doesn't work as expected, print the values you're working with:
$pro.test $pro.test.class $pro.test.sortOrder $pro.test.sortOrder.class
#foreach ($it in ${pro.test.sort(${pro.test.sortOrder})})
...
#end
What does that print? Is $pro.test
really a set? Is $pro.test.sortOrder
a valid sort order specification, as expected by SortTool
?
If $pro.test
is a standard implementation of the Set
interface, then there's no sort
or sortOrder
method. Assuming that the Velocity SortTool
is available under the $sorter
variable, you could try, for starters:
#foreach ($it in $sorter.sort($pro.test))
...
#end
Now, what exactly is $pro.test.sortOrder
supposed to be? If it's a property, then note that for security reasons Velocity doesn't allow access directly to properties, it can only call methods. As a means of simplifying the syntax, $pro.test.sortOrder
will actually try to call one of these: getsortOrder()
, getSortOrder()
, get("sortOrder")
, issortOrder()
, isSortOrder()
.
这篇关于速度foreach排序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!