6

I have a command bean (FooList) which has a property which is a collection (a List of Foo beans).

I'm trying to create a form which can edit all the Foos at once. I have found many examples of how to do this using JSP, but I'm having trouble translating these to Freemarker syntax.

In my Freemarker template, I can easily iterate over the collection:

[#list fooList.foos as foo]
...
[/#list]

I can also refer to a particular Foo by index:

[@spring.bind "fooList.foos[0].name" /]
<input type="text" name="${spring.status.expression}" value="${spring.status.value?default('')}"/>

However, I haven't yet worked out how I can do both at the same time, to bind all the Foos to form elements.

Here's one naïve attempt which failed:

[#list fooList.foos as foo]
    [@spring.bind "fooList.foos[foo_index].name" /]
    ...
[/#list]

(On its own, ${foo_index} works inside the loop.)

Can anyone point me in the right direction?

Thanks.

Ben James
  • 121,135
  • 26
  • 193
  • 155

3 Answers3

3

Just had the same problem. This worked for me:

[#list fooList.foos as foo]
  <#assign item>fooList.foos[${foo_index}].name</#assign>
  [@spring.bind item /]
  ...
[/#list]
Stefan Haberl
  • 9,812
  • 7
  • 72
  • 81
  • On a side note, I find Spring's Freemarker support somewhat buggy sometimes. Which really is a shame, as IMHO I regard Freemarker far more advanced and far more simple to use in general when compared to JSPs – Stefan Haberl Dec 21 '10 at 09:10
2

Try,

[#list fooList.foos as foo] 
    [@spring.bind "foo.name" /] 
    ... 
[/#list] 

The foo in that example will reference each item in the list one by one, according to the freemarker documentation on the list directive.

Andy
  • 8,841
  • 8
  • 45
  • 68
  • Thanks, but that was the very first thing I tried. If you're sure that this is the correct solution then I can provide the tracebacks (but not right now). – Ben James Nov 17 '10 at 19:50
  • Sorry but I can't actually try it, I don't see why that wouldn't work though. Have you seen this example, (14.4.5.2) ? http://static.springsource.org/spring/docs/2.0.x/reference/view.html – Andy Nov 17 '10 at 20:08
  • Yes, I have seen that example, but it only shows a simple example in Freemarker syntax. I am looking for an example of binding a collection of arbitrary size. – Ben James Nov 17 '10 at 21:32
0

I think it should be as follows:

[#list fooList.foos as foo]
    [@spring.bind "fooList.foos[" + foo_index + "].name" /]
    ...
[/#list]