How to dynamically generate multiple forms on a single jsp page using spring?

I have a scenario in which I intend to generate the same form several times. My application is based on spring 3.0 framework.

Scenario: I mainly develop this application for a transport company where there is a requirement to plan loads), which driver carries what load and when and from what origin to which destination). Now the problem is that the loads will not be delivered directly from the source to the destination, there will be a split in the delivery, for example, one driver will carry goods to a certain point from the origin, and another driver will transfer them from this point to the destination. But the number of splits can vary greatly.

So, I need to generate several forms dynamically depending on the number of partitions in order to plan loads like

Enter first splits information -------------------------------- form1 ---------------------------------- Enter second splits information -------------------------------- form2 ------------------------------- submit button 
+4
source share
2 answers

You can do something as below

First, in any of your jquery functions on the jsp page, add the following code,

 for(var i=0; i<lcount;i++){ //lcount is number of splits. $('#tload'+i).load('url of controller class that helps in loading form'); } 

Write divs on the same jsp page with id like 'tload1','tload2','tload3' and so on.

 <div id="tload1"></div> <div id="tload2"></div> <div id="tload3"></div> ............ ............ so on 

whenever you return from your controller, return to the jsp page containing the iframe that loads your form. Using the .load() function, as shown above, will automatically load the iframe containing your forms into the appropriate div tags . This means that your problem is intercepted.

+4
source

Here are a few options:

  • Pure JavaScript approach - I would use a JS library such as jQuery, but mostly JS, which will add the necessary sections to the form. See my answer here .
  • Feedback approach. Create a JSTL form using the "splits" list in the model attribute. When the user clicks "Add Split", you invoke the same URL, passing an optional parameter to indicate how many partitions should be. He adds a new one to the list, returns to your view.

I personally like the first one, as it reduces the number of calls to the backend. The only thing you should observe is if you have the Add and Remove button. If you remove material from a list that is supported by a map / list in your model attribute, you need to β€œclear” those records that may still be there when you finally submit your form.

0
source

All Articles