1

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 );
        }
}

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

in my scenario i need multiple objects with one form, so i cannot save one by one
0

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 );
        }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.