以速度访问嵌套集合

时间:2014-08-22 14:47:15

标签: java loops templates nested velocity

我有这个速度模板:

<userTestData>

#foreach( $user in $userList )
<user>
    <imsi>$user.imsi</imsi>
    #foreach ( $subscriptionToGroupMap in $user.subscriptionToGroupsList )
    <subscriptionToGroup>
        #foreach ( $key in subscriptionToGroupMap.keySet() )
        <$key>$subscriptionToGroupMap.get($key)</$key>
        #end
    </subscriptionToGroup>
    #end
</user>

#end
</userTestData>

但是在尝试时我收到以下错误消息:

16:23:58,872 ERROR [org.jboss.as.ejb3.invocation] (http-/127.0.0.1:8080-1) JBAS014134: EJB Invocation failed on component ... for method public void ...) ...: javax.ejb.EJBException: org.apache.velocity.exception.ParseErrorException: Invalid arg #2 in directive #foreach at userTestDataTemplate.vm[line 10, column 21]

这基本上指向这一行:

#foreach ( $subscriptionToGroupMap in $user.subscriptionToGroupsList )

其余代码如下:

ArrayList userList = new ArrayList();

    for(...) {
        Map user = new HashMap();
        user.put("imsi", i);

        ArrayList subscriptionToGroupsList = new ArrayList();

        Random rnd = new Random();
        int numberOfSubscriptionsToGroups = (rnd.nextInt(1000) % (BucketType.values().length)) + 1;
        LOG.log(Level.INFO, "...: numberOfSubscriptionsToGroups {0}", numberOfSubscriptionsToGroups);
        for(int j = 0; j < numberOfSubscriptionsToGroups; j++) {
            Map subscriptionToGroupMap = new HashMap();

            subscriptionToGroupMap.put("name", ...);

            subscriptionToGroupMap.put("priority", rnd.nextInt(Integer.MAX_VALUE) % (Integer.MAX_VALUE));

            ...
            Date startDate = new Date(dateGeneratorHelper);
            String stringFormattedStartDate = new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss").format(startDate);
            subscriptionToGroupMap.put("startDate", stringFormattedStartDate);

            Date endDate = new Date(dateGeneratorHelper + dateGeneratorHelperHelper + 1);
            String stringFormattedEndDate = new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss").format(endDate);
            subscriptionToGroupMap.put("endDate", stringFormattedEndDate);

            subscriptionToGroupsList.add(subscriptionToGroupMap);                
        }

        user.put("subscriptionToGroupsList", subscriptionToGroupsList);

        userList.add(user);           
    }

    String debugOutput = null;
    for (Iterator it = userList.iterator(); it.hasNext();) {
        HashMap userMap = (HashMap)it.next();
        debugOutput += "\n" + userMap.get("imsi") + "\n";
        ArrayList stgsList = (ArrayList)userMap.get("subscriptionToGroupsList");
        for (Iterator jt = stgsList.iterator(); jt.hasNext();) {
            Map stgMap = (HashMap)jt.next();
            for (Iterator nt = stgMap.entrySet().iterator(); nt.hasNext();) {
                Map.Entry<String, ArrayList> entry = (Map.Entry<String, ArrayList>)nt.next();
                debugOutput += "\t<" + entry.getKey() + ">" + entry.getValue() + "</" + entry.getKey() + ">\n";
            }
        }
    }

    ServletContext servletContext = (ServletContext)webServiceContext.getMessageContext().get(MessageContext.SERVLET_CONTEXT);
    VelocityEngine ve = new VelocityEngine();
    ve.setApplicationAttribute("javax.servlet.ServletContext", servletContext); 
    ve.setProperty("resource.loader", "webapp"); 
    ve.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.servlet.WebappLoader"); 
    ve.setProperty("webapp.resource.loader.path", "/WEB-INF/classes"); 
    VelocityContext velocityContext = new VelocityContext();
    velocityContext.put("userList", userList);
    Template t = ve.getTemplate("userTestDataTemplate.vm");
    StringWriter writer;
    writer = new StringWriter();
    t.merge(velocityContext, writer);

我从Java代码中获得了正确的调试输出,因此信息就在那里。

你知道为什么速度不能接受$ user Map中列表的访问权限吗?

1 个答案:

答案 0 :(得分:2)

嗯,你的速度上下文包含一个对象“userList”,它是List,你可以从中得到$user:好的。 $userMap,而.又包含地图列表。

因此错误是正常的:velocity使用#foreach ( $subscriptionToGroupMap in $user["subscriptionToGroupsList"] ) 表示法来访问对象的方法或bean的属性,而不是地图的元素。

你必须写:

$

除了以后缺少{{1}}行之外(参见上面的评论),模板的其余部分看起来还不错(但我没有测试它......)