问题描述
到目前为止,我试过了
<$ p $在$ {pro.test} .sort($ {pro.test.sortOrder}))
...
#end
但是这是抛出错误,我还试着用
pre $ $ pro.test.sort($ { ...
#end
这一次没有错误,但是控制不是进入循环,
,那么没有 sort 或 sortOrder 方法。假设在 $ sorter 变量下可以使用Velocity SortTool ,对于初学者来说,可以尝试一下:
#foreach($ sorter.sort($ pro.test))
...
#end
现在, $ pro.test.sortOrder $ c应该是?如果它是一个属性,那么请注意,出于安全原因,Velocity不允许直接访问属性,它只能调用方法。作为简化语法的方法, $ 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排序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!