Submit a List from a form generated with JavaScript with a Spring controller

Viewed 2382

I'm building an app with Spring Boot, the MVC pattern, and Thymeleaf as template engine. I have a form that generates a list with JavaScript, and a controller with a @ModelAttribute expecting a list, to be saved into the database.

At the JavaScript side I collect the items with this array:

groupList = [];
groupList.push(inputValue);

function getGroupList(){
return groupList;
}

In the html I'm trying to assign the array value to a hidden input field:

<input type="hidden" class="invisible" id="groupList" th:field="*{groupList}"/>

With this inline function:

<script th:inline="javascript" type="text/javascript">
      $(function() {
            var groupListCollected = getGroupList();
            $("#groupList").val(groupListCollected);
       });
</script>

My problem at this point, is that I don't find the way to collect the array, and pass it to the Controller as a list.

This is the value that the console shows, for the input field:

<input type="hidden" class="invisible" id="groupList" name="groupList"
       value="[object HTMLInputElement]">

Any advice to face this issue, would be much appreciated. Thanks in advance.

1 Answers
Related