简体   繁体   中英

SpringMVC how to populate part of the model from a JS array on form submition?

Probably the following description of the issue I'm encountering will be a bit vague, but I'll try to explain as good as I can.

I have a form that is being submitted to a Spring mvc controller. Additionally to the information from that form I want to populate an array of the model exposed by the controller, from an array I created in JavaScript.

On the server side, I want take the data received from the form and that array and persist it in a DB.

How do I populate the array I have on the server side, from the array created in JS when I submit the form?

Here is how I would do that :

Add a javascript listener to the submit event of the form. Before submitting the form, add fields to it with the values of the javascript array.

On the server side, make sure that the model as a collection attribute corresponding to these added fieds.

You could also serialize the array (in JSON for example) and put it in one field, and deserialize it on the server side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM