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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 14:42:40

问题


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]


来源:https://stackoverflow.com/questions/4207871/freemarker-syntax-for-a-form-for-a-collection-of-objects-spring-3-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!