2

我正在尝试以速度在列表中进行搜索。我有一个国家代码列表,我设置如下:

#set($Europe=(['ee','ja','ku','aa' ])  

我有一个变量($cntryCode),已经设置了我想与列表中的每个项目进行比较,以查看该国家/地区是否来自该地区(在本例中为欧洲,或者不是)。

我做了:

#foreach ($cntryCode in $Europe)
#set ($region="Europe")
#end

但它不起作用。另外,我试过:

#if ($Europe.contains($cntryCode)
#set($regio="Europe")
#end
#end

但也不起作用。

这是我的第一个问题,然后我需要为某些国家/地区设置多个区域......知道如何做到这一点吗?

4

1 回答 1

3

不确定您到底要做什么,但对我有用的是以下内容,即遍历列表并检查是否找到该元素:

## pre-set by some other code
#set($cntrCode = 'ja')

#set($Europe=(['ee','ja','ku','aa' ]) ) 

#foreach ($eu in $Europe)
#if($eu.equals($cntrCode))
    found country code $cntrCode in $Europe
#end
#end

在这种情况下,$Europe 似乎是 java.lang.ArrayList 类型,所以下面的也是有效的

#if($Europe.contains($cntrCode))
    found country code $cntrCode in $Europe.class
#end

我希望这可以帮助您入门,否则Velocity 用户指南和其他文档也应该为如何在 Velocity 中处理这些事情提供有用的信息。

于 2012-11-27T13:08:54.967 回答