Skip to content
Advertisement

Serving HTML pages in a Spring Boot application

I have a Spring Boot Application. I am trying to pass a variable to a HTML page but unfortunately I cannot seem to do it, on application start nothing is rendered except the static text: “WORLD”. My controller is as below:

JavaScript

And my index.html:

JavaScript

I was looking for an answer and I found these articles:

How to serve .html files with Spring

How to serve html files with Spring

How to handle static content in Spring MVC?

These are useful examples, but in my project I don’t have files such as: mvc-dispatcher-servlet.xml, web.xml, or even the whole WEB-INF directory.

The files I have after creating a Spring MVC Project are in the screenshot below. Should I add the files mentioned in the above literature to fix this issue or what?

My Project view via IntelliJ IDEA:

Project Structure

My pom.xml file:

JavaScript

Advertisement

Answer

You’re using Thymeleaf as your templating language, and you should read the documentation for it. In Thymeleaf, to substitute a variable, you’ll use an attribute on the HTML tag, so something like this:

JavaScript
Advertisement