Freemarker syntax for a form for a collection of o

2019-06-20 15:02发布

问题:

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.

回答1:

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]


回答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.



回答3:

I think it should be as follows:

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