In my Spring MVC application I need to implement a dynamic questionnaire form: I have N questions and for each I have 3 options.
So in my page I'll have something like this:
| Question 1 | 1 | 2 | 3 |
| Question 2 | 1 | 2 | 3 |
| Question 3 | 1 | 2 | 3 |
| ... | 1 | 2 | 3 |
| Question N | 1 | 2 | 3 |
Questions are stored in a database and for the options I'll use radio buttons.
I'll use a forEach
tag to creare dynamic rows, but I don't know how to post data and handle ModelAttribute
binding in this scenario...
Which could be a good structure for my model attribute class? Is it possible to use binding for a dynamic form with Spring MVC?
you can do,
i am considering
Question
class like:and
Option
class like:and one
QuestionsModel
class for form binding like:and inside Controller class
GET
handler method, populate questions for example:finally in jsp page use
<form:hidden..
to keep old values, and render form elements like:you can receive binding and model in POST like:
This class is my model attribute:
And:
I populate
questions
list before I put it into model.In my page I use this code:
Posting this form I get a fully populated
questionnaire
instance in my controller.NOTE I found this post very helpful to solve my problem.