Aleix Alcover
Aleix Alcover

Reputation: 649

Passing a list generated with Javascript to a Spring controller

My app is being built with Spring Boot using a MVC pattern, and as template viewer I use Thymeleaf.

I'm generating a dynamic list with Javascript in a form, which I need to collect as a List with the controller.

I have tried to solve it with a @RequestParam, but generating the list with Javascript, as far as I'm concerned, I can't set the Thymeleaf tags.

This is the list:

<ul id="addItemList">
   <li class="list-group-item" id="group" name="group" value="Outdoors">Outdoors</li>
   <li class="list-group-item" id="group" name="group" value="Entertainment">Entertainment</li>
</ul>

Any indication on which approach I should take, would be much appreciated.

Thanks in advance.

Upvotes: 1

Views: 232

Answers (3)

Aleix Alcover
Aleix Alcover

Reputation: 649

In the end I solved this issue with ajax. I had a button to add an element to the list, which was being made with Javascript. I added a jQuery $.post function to save the item, each time that a new one was added to the list by selecting that button. I didn't find the way to move the whole list from javascript, to the Spring Controller.

Upvotes: 1

Nur Zico
Nur Zico

Reputation: 2447

Follow the process:

  1. Get the list from controller using ajax
  2. Parse the data then generate li with the retrieved values and update the html of id addItemList

Upvotes: 0

TOUZENE Mohamed Wassim
TOUZENE Mohamed Wassim

Reputation: 712

Create a Model having a List as property, and pass it as @ModelAttribute in your controller.

Upvotes: 1

Related Questions