速度foreach排序列表 [英] Velocity foreach sort list

查看:138
本文介绍了速度foreach排序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



到目前为止,我试过了

<$ p $在$ {pro.test} .sort($ {pro.test.sortOrder}))
...
#end

但是这是抛出错误,我还试着用

pre $ $ pro.test.sort($ { ...
#end

这一次没有错误,但是控制不是进入循环,

$ {.. 。} 是帮助Velocity解析器确切知道你认为是Velocity代码的一种方法。通常情况下,当速度看到 $ something.somethingElse.somethingMore 时,它会尝试尽可能多地解析它,直到它看到一个单词中断字符,例如空格或逗号。当它看到 $ {something.somethingElse} .somethingMore 时,它只会读取到匹配的} 作为实际的代码,休息是纯文本。这意味着 $ {pro.test} .sort(...)认为 .sort(...)作为纯文本应该打印,所以它不会把它称为一种方法。这就是为什么第一个例子失败并出现错误。你只需要使用外部指令的形式语法,但是,你可以删除所有的 {} 当$ #foreach(...)。



时调用其次, #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 $ 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,

reference

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆