开发者

Freemarker syntax for a form for a collection of objects (Spring 3 MVC)

开发者 https://www.devze.com 2023-01-26 02:50 出处:网络
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 t

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.


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]


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.


I think it should be as follows:

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

精彩评论

暂无评论...
验证码 换一张
取 消