Ahmad
Ahmad

Reputation: 1474

Multiple Objects of pojo class in 1 html form spring mvc?

i am working with Spring where my form fields are same with attribute fields so when i submit form it directly maps to database fields and save the data it works perfectly, but what if i want to save multiple objects with one form,

HTML:

<form>
  Payment:<br>
  <input type="text" name="payment"><br>
  Date:<br>
  <input type="date" name="paymentDate">
</form>

POJO:

public class ProjectPayment
{
    private Double payment;
    private Date paymentDate;
    // setters and getters
}

Controller:

@RequestMapping(value = "/addnewproject", method = RequestMethod.POST)
     public @ResponseBody String SaveProject(ProjectPayment projectPayment) {
     projectPaymentService.saveProjectPayment( projectPayment);
}

this works perfectly, but now in my some scenario i need multiple objects dynamically then how to save in database, how controller should look like

for example:

Now my Form is

<form>
  Payment:<br>
  <input type="text" name="payment"><br>
  Date:<br>
  <input type="date" name="paymentDate">
Payment:<br>
  <input type="text" name="payment"><br>
  Date:<br>
  <input type="date" name="paymentDate">
Payment:<br>
  <input type="text" name="payment"><br>
  Date:<br>
  <input type="date" name="paymentDate">
Payment:<br>
  <input type="text" name="payment"><br>
  Date:<br>
  <input type="date" name="paymentDate">
</form>

Now this form have multiple objects of ProjectPayment class but it saves only one object please tell me how my controller should like, i have done like this but it occurs exception

Controller:

@RequestMapping(value = "/addnewproject", method = RequestMethod.POST)
     public @ResponseBody String SaveProject(ProjectPayment[] projectPayment) {
     for(ProjectPayment propay : projectPayment)
        {
            projectPaymentService.saveProjectPayment( propay );
        }
}

Upvotes: 1

Views: 1215

Answers (2)

Viraj Dhamal
Viraj Dhamal

Reputation: 5325

Simple way to solve this problem is create a ViewModel.

e.g.

public class ProjectPaymentViewModel
{
  private List<ProjectPayment> listProjectPayment;    
  // setters and getters
}

Use this view model on web page and controller

<form>
 Payment:<br>
 <input type="text" name="listProjectPayment[0].payment"><br>
 Date:<br>
 <input type="date" name="listProjectPayment[0].paymentDate">
 Payment:<br>
 <input type="text" name="listProjectPayment[1].payment"><br>
 Date:<br>
 <input type="date" name="listProjectPayment[1].paymentDate">
 Payment:<br>

</form>

On controller

@RequestMapping(value = "/addnewproject", method = RequestMethod.POST)
 public @ResponseBody String SaveProject(ProjectPaymentViewModel projectPaymentViewModel) {
 for(ProjectPayment propay : projectPaymentViewModel.getListProjectPayment())
    {
        projectPaymentService.saveProjectPayment( propay );
        }
}

Upvotes: 0

Tue Dang
Tue Dang

Reputation: 41

I can understand that you want to post data from a grid/table, however it's too ambiguous to determine which field map to which object. Example:

  • field1
  • field2
  • field3 ==>Map to object at index 1 or 2?
  • filed1

So you think field3 should map to array index=1 or index=2? So I suggest you should submit one by one to solve this issue.

Upvotes: 1

Related Questions