将对象列表传递给Freemarker,然后循环 [英] Passing a List of Objects to Freemarker and then Looping

查看:524
本文介绍了将对象列表传递给Freemarker,然后循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经熟悉FreeMarker,它是Java的模板引擎.

I've been acquainting myself with FreeMarker, a template engine for Java.

我已经达到了可以通过哈希映射将对象传递给模板引擎的地步.没问题.但是,一旦我尝试将任何类型的一组多个对象传递给FreeMarker,它就会给我一个freemarker.template.TemplateException,并抱怨说它预期的集合或序列.作业已评估为freemarker.template.SimpleHash".

I got to the point where I am able to pass an object to the template engine through a Hash Map. That works alright. But as soon as I try to pass any sort of set of multiple objects to FreeMarker it gives me a freemarker.template.TemplateException and complains that it "Expected collection or sequence. jobs evaluated instead to freemarker.template.SimpleHash".

根据我在各种资源中阅读此书的理解,这是可以预料的.

From what I understand from reading up on this in the various resources, this is to be expected.

现在,我已经完成了很多腿部工作,并且发现许多人在评论如何解决此问题.但是,坦率地说,(a)对于许多示例,他们的建议在我的案例中的适用程度尚不明确-尽管我已经了解Java基础已有相当长的一段时间了,但我对其中的某些体系结构还是很陌生的关于Java Web应用程序,(b)我对哪种方法是最好的方法感到困惑.

Now, I have done much of the leg work and found a number of people commenting on how to get around this. But, quite frankly, (a) for many of the examples it was unclear as to how exactly their advice applies in my case--even though I've known Java basics for quite a while I'm pretty new to some of the architecture pertaining to Java web apps and (b) I'm confused as to which of the approaches was the best approach.

在最简化的层次上,我要做的基本上是这样:

All I want to do, at the most simplified level, is basically this:

  1. 我有一个简单的Servlet.

  1. I have a simple Servlet.

我有一个简单的类(在此示例中为Invoice),其中包含一些方法和属性.

I have a simple class (for this example named Invoice) with a few methods and properties.

我想让我的servlet(通过某种方式)通过FreeMarker的处理方法呈现这些对象实例(或这些对象的视图)的列表/数组/序列/哈希图.

I want to have my servlet (in some fashion) present a list/array/sequence/hashmap of instances of these objects (or views of those objects) via FreeMarker's process method.

我想让我的.ftl模板遍历list/array/sequence/hashmap和显示方法结果,如下所示:

I want to have my .ftl template do a loop through the list/array/sequence/hashmap and display method results, something like this:

< # list invoices as invoice> 
Invoice note: ${invoice.getNote()}, Invoice Amount:${invoice.getAmount()} 
< / # list>

现在,我不一定要寻找快速&对此的肮脏解决方案.我是FreeMarker的新手,但是我想以一种优雅而又好的设计的正确方式来做到这一点.因此,我愿意完全重新考虑这种方法.有人可以帮我看看我需要做什么才能使类似的东西起作用吗?

Now, I'm not necessarily looking for the quick & dirty solution to this. I'm new to FreeMarker, but I want to do this in the proper way that is elegant and good design. So I'm open to completely rethinking this approach. Can someone help me see what I need to do to get something like this to work?

推荐答案

工作"真的是一个集合吗?请在您创建和处理模板的地方张贴一段代码.

Is "jobs" really a collection? Please post a snippet of code where you are creating and processing your template.

我刚刚写了一个快速测试来检查:

I just wrote a quick test to check:

public void testFreeMarker() throws Exception {

    List<Invoice> invoices = Arrays.asList(
       new Invoice( "note1", "amount1" ), 
       new Invoice( "note2", "amount2" ) );
    Map<String, Object> root = new HashMap<String, Object>();
    root.put( "invoices", invoices );
    StringWriter out = new StringWriter();

    Configuration cfg = new Configuration();
    cfg.setClassForTemplateLoading( FreemarkerUtils.class, "/templates" );
    cfg.setObjectWrapper( new DefaultObjectWrapper() );
    Template temp = cfg.getTemplate( "listTest.ftl" );
    temp.process( root, out );

    System.out.println( out.getBuffer().toString() );
}

模板就是:

<#list invoices as invoice>
 Item: ${invoice.note} - ${invoice.amount}
</#list>

结果符合预期:

Item: note1 - amount1
Item: note2 - amount2

这篇关于将对象列表传递给Freemarker,然后循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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