I need to submit two forms with one submit button, so i did JS function :
var submitAllForms = function(){
var paymentForm = $("#paymentsDetailsForm").serialize();
var orderForm = $("#orderForm").serialize();
$.ajax({
method: "post",
url: "/order",
dataType: 'json',
data: { paymentsDetailsForm : JSON.stringify(paymentForm), orderForm : JSON.stringify(orderForm) },
success: alert('form has been sent')
});
}
There are my forms:
@Data
public class PaymentDetailsForm implements Serializable {
private Date storageDate;
private String paymentMethod;
...
}
@Data
public class OrderForm implements Serializable {
private String company;
private String sizeOfMove;
...
}
There is my controller:
@RequestMapping(value = "/order", method = RequestMethod.POST)
public ModelAndView handleOrder(String paymentsDetailsForm, String orderForm) {
// How to cast paymentsDetailsForm to PaymentsDetailsForm
// How to cast orderFormto OrderForm
ModelAndView model = new ModelAndView();
model.addObject("paymentsDetailsForm", new PaymentDetailsForm());
model.setViewName("/order");
return model;
}
}
So in the controller, parameter "orderForm" looks like: "company=ROYAL_MOVING&sizeOfMove=STUDIO..."
How can i convert string parameters to OrderForm and PaymentsDetailsForm? Or this issue(submit and handle multiple forms in one method) may be solved some another way?