Skip to content
Advertisement

Recieve a Map from a HTML form with Thymeleaf

I’m working on a project with Spring Web and Thymeleaf (this is my first time using Thymeleaf) and I have this requirement:

The user needs to see a single form to fill a table with 32 rows and 4 columns, all at once.

Edit. Each row has a fixed key (just for clarification)

My first (naive) approach was to make inputs that look like this

JavaScript

This works well on the HTML side but, on the controller, I have a DTO with all 128 fields

JavaScript
JavaScript

This currently works, but I’m pretty sure there’s a better way.

Is there a way I can modify the bindings so I can receive something like this?

JavaScript

Where Key is an enum and SimplerDTO is a wrapper for the four values?

As said before, this is my first time with Thymeleaf, son any pointer will be well received.

Thanks in advance.

Advertisement

Answer

You can get all the post parameters in a single map by including an argument in your PostMapping function saveTable like:

JavaScript

That will have all 256 values for your use.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement