2

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?

1 Answer 1

2

It's a JSON string, so don't worry. You can search in Google, as there are many ways available to deserialize a JSON string into an object.

One more way is that the first split string from & sign and then again split each group from = sign and now you can map that values with your object in a loop.

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

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.