如何在速度模板中使用“ for”循环?

| 我只是在Google搜索“ for循环”,但看起来速度只具有“ foreach”。 如何在速度模板中使用\'for loop \'?     
已邀请:
只有
#foreach
。您必须在上下文中添加一些可迭代的内容。例如。使
bar
可用为数组或
Collection
某种形式:
#foreach ($foo in $bar)
    $foo
#end
或者,如果您想遍历一个数字范围:
#foreach ($number in [1..34])
    $number
#end
    
想要补充的是,可以从特殊的can5ѭ属性访问foreach循环内的迭代信息:
#foreach ($foo in $bar)
    count: $foreach.count
    index: $foreach.index
    first: $foreach.first 
    last:  $foreach.last
#end
(上次我检查
last
包含一个错误)     
当我尝试循环列表时,我找到了解决方案。 将列表放在另一个类中,并为列表obj创建getter和setter。 例如
public class ExtraClass {
    ArrayList userList = null;

    public ExtraClass(List l) {
        userList = (ArrayList) l;
    }

    public ArrayList getUserList() {
        return userList;
    }

    public void setUserList(ArrayList userList) {
        this.userList = userList;
    }

}
然后对于速度上下文,将Extraclass作为输入。 例如。
  ExtraClass e = new ExtraClass(your list);
VelocityContext context = new VelocityContext();
context.put(\“ data \”,e); 在模板内
#foreach ($x in $data.userList)
        $x.fieldname    //here $x is the actual obj inside the list
    #end
    

要回复问题请先登录注册