Posts

Showing posts from April, 2020

DTOs conversion with reflection example - applied on Thymeleaf (or how to get read of Pojo's conversion boilerplate)

I was playing around with Thymeleaf the other day and I wanted to pass all values of my ModelAttribute POJO (Person.class in the example) to Spring's UI Model so I can use the values to the template. (Example to follow).


A form is filled, and the values are passed to a controller that will render them to a new view (html) page. Nothing complex or useful, but just an alternative to hello world if you will.

The ModelAttribute Person.class. Ignore the @ModelElement annotations for now.

The main page that will render a simple form with 1 textfield (name),  one numeric field (age), one drop down menu to restrict the input values of a java enum (sex) and of course the submit button.

When we click the submit button the following controller will be called, passing the values via the Person object. We then are passing them to Spring's model attribute so we can use them to our template. Lines 4-6 should belong in a different class but for simplicity I am putting them in the controlle…