Question

I have a scenario where in I'm suppose to generate same form multiple times. My application is based on spring 3.0 framework.

Scenario: Basically I'm designing this application for transportation company, where there is a requirement to schedule the loads )which driver carries which load and when and from which origin to which destination). Now, the problem is some times loads will not be directly delivered from origin to destination, there will splits in delivery, for example, one driver will carry loads up to some point from origin and again another driver carries it from that point to destination. But the number of splits may vary very time.

So i need to generate multiple form dynamically based on number of splits to schedule the loads like

Enter first splits information
--------------------------------
form1
----------------------------------


Enter second splits information
--------------------------------
form2
-------------------------------

submit button
Was it helpful?

Solution

You can do something as below,

First in any of your jquery function in your jsp page add 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 some divs in same jsp page with id as 'tload1','tload2','tload3' so on.

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

whenever returning from your controller return to jsp page that contains iframe which loads your form. By Using .load() function as shown above will automatically load iframe containing your forms into respective div tags.That's it, your problem is sloved.

OTHER TIPS

Here are some options:

  1. Pure JavaScript approach - I would use a JS library, like jQuery, but basically have JS that will add the needed sections to the form. See my answer here.
  2. Round-trip approach - Have JSTL build the form using a list of "splits" in the model attribute. When the user hits "Add Split", you make a call to the same URL, passing in an optional parameter to indicate how many splits should be there. It adds a new one to the list are returns back to your view

I personally like the first one, as it reduces the number of calls to the backend. The one thing you have to watch for is if you have an Add and Remove button. If you remove stuff from a list that is backed by a map/list in your model attribute, you need to "clean out" those entries else they could still be there when you finally submit your form.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top