A N M Bazlur Rahman
A N M Bazlur Rahman

Reputation: 2300

How can I handle multiple and dynamic forms in spring mvc

I have to 4 objects, Group, sections and Questions and their options. Every Group has different Sections and Sections have Multiple Questions and Questions have options. Now I have to design form input system so that every group and sections can be covered up step by step. I’m doing all this in spring mvc.

Can you tell me a way, how can I solve this problem?

Upvotes: 3

Views: 2241

Answers (1)

Jerome Dalbert
Jerome Dalbert

Reputation: 10405

You can surely do that in Spring MVC thanks to easy list binding.

Spring MVC allows a big lot of freedom, so basically if you only use this framework, you will have to come up with a solution from scratch.
Here is a use case and a solution. It is a bit tough to implement, as it is from scratch. Feel free to adapt it to your specific needs, add whatever fancy UI framework you want, but you should get a general idea. You can skip to part III for a quick answer.

Let's say you want to create/edit a group in one single page :

  • you want to edit/add sections to the group
  • you want to edit/add questions to any section
  • you want to edit/add options to any question

I. Page design :

  1. You have a JSP Group page with one big single form (again this is just an example)
  2. There will be buttons to add nested stuff. When clicked, some javascript will show a blank form allowing with the information of the stuff. This blank form will itself have buttons to add nested nested stuff to it, and so on (until the stuff is an option).
  3. The design is the same for existing stuff that you want to display/edit, except that the forms won't be blank
  4. You are able to tell whether you are creating or editing the current group (context of the page or javascript/html tricks).
  5. There may be a list of existing sections you could "drag & drop" inside a group thanks to some javascript/jQuery

II. Code design :

  1. You have a GroupController corresponding to the JSP
  2. The Group object has a List<Section> sections attribute, the Section object has a List<Questions> questions attribute, etc.
  3. You have methods createGroup and editGroup
  4. When submitting your JSP form and calling createGroup or editGroup, you are able to know if the submitted elements of the group already exist, so you can decide to add or edit/update them in the DB. All this logic could be done in another class, like a Spring service.
  5. You always provide a possible list of existing sections (by adding this list to the model in both methods, or better yet, by doing a single method annotated with @ModelAttribute). This list could be computed in another class (a Service/DAO/both which taps the DB in the end).

III. The magic : binding the JSP form with the Java controller :

In the page you'll have a <form:form commandName="group">, and in the controller methods parameters you'll have a @ModelAttribute("group") Group group.

Now, to submit the name of the very first option, you would have this in the JSP :

<form:input path="sections[0].questions[0].options[0].name" />

(or the equivalent in html generated by some javascript).

Upvotes: 2

Related Questions