Form'object is List<BeanName>, how to solve it?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Form'object is List<BeanName>, how to solve it?

yifan
This post was updated on .
HI,all
I have a problem.
I want bind List<BeanName> to Form.
For example, object beanList is type of List<BeanName> and BeanName has a attribute named 'id'
<form th:object="${beanList}">
<fieldset>
<input type="text" th:field="?">
</fieldset
</form>

How can I get the beanName.id in th:field?  Or what in the th:object of Form must be a Formbean?
no way
Reply | Threaded
Open this post in threaded view
|

Re: Form'object is List<BeanName>, how to solve it?

danielfernandez
Administrator
You will need you List<BeanName> to be a property of another object (like e.g. "form", with a property named "beanNames" with at least a getter), and add this "form" object to the model before executing the view.

Then if the number of elements in your list is 'beanSize' (which could be "form.beanNames.size()" or computed anyhow else), you can do:

<form th:object="${form}">
  <fieldset>
    <input type="text" th:each="index : ${#numbers.sequence(0,beanSize - 1)}" th:field="*{beanNames[__${index}__].id}" />
  </fieldset>
</form>

This should create a series of input fields with names "form.beanNames[0].id", "form.beanNames[1].id", etc.