0 votes

Passing data from Java to JavaScript with Thymeleaf

Would it be possible to pass information to the interface from a java class without using JSP? Like a push method but in the other direction. All this with Thymeleaf. At first I thought of passing this information as parameters in the URL and retrieve them, but with the archetype used this option is discarded.

The application adds records to a SQLServer table and the idea was to be able to send these records as a string to the HTML interface, being able to output them in a window, as a console.


When you use Thymeleaf, the pages are .html not jsp, there are several ways to pass information to the view, you can do it with a json object, or with the Spring Model object, it all depends on what way you are calling your controller.

When you use the model class it is something like this.

Model model; 
Prueba  prueba =  new Prueba();
model.addAttribute("prueba", prueba );

this model class allows to pass all kind of data, from primitive to complex objects, it works as a kind of HashMap().

hope to help greetings


Patricio Fernandez Points 89

Hi sorry for not answering sooner, to recover in the view you must do something like this.

let's take the example Test

has a Private Integer note attribute;

then you recover the value in this way


<div class="form-group col-xs-3">
                            <label for="name">Nota</label>
                            <input type="text"   th:value="${prueba.nota}" class="form-control"/>

introducir la descripciĆ³n de la imagen aquĆ­


HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: