Spring-boot and Thymeleaf not proecssing templates

18,858

Solution 1

These are the 2 options based on Thymeleaf Tutorials

Inlining Expression

- MainController.java

package com.latencyzero.hoa;

import org.springframework.stereotype.Controller;    
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/")
class MainController
{
    @RequestMapping(method = RequestMethod.GET)
    ModelAndView
    index()
    {
        ModelAndView mav = new ModelAndView("index");
        mav.addObject("version", "0.1");
        return mav;
    }
}


- index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8"/>
    <title>HOA v[[${version}]]</title>
</head>
<body>
<h1>HOA v[[${version}]]</h1>
</body>
</html>

Natural Template

- MainController.java

package com.latencyzero.hoa;

import org.springframework.stereotype.Controller;    
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/")
class MainController
{
    @RequestMapping(method = RequestMethod.GET)
    ModelAndView
    index()
    {
        ModelAndView mav = new ModelAndView("index");
        mav.addObject("hoa_version", "HOA v0.1");
        return mav;
    }
}


- index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8"/>
    <title th:text="${hoa_version}">Default Title</title>
</head>
<body>
<h1 th:text="${hoa_version}">Default Header</h1>
</body>
</html>

Solution 2

I had faced the similar problem with the @RestController when I replaced it with @Controller it worked as expected.

Share:
18,858
Rick
Author by

Rick

I develop hardware (sometimes) and software (a lot of the time) for a wide variety of products and projects. I have a substantial machine shop including a large CNC router on which I designed the motion control system. I prefer writing code for in Swift and Xcode, but have written Verilog, assembler, and numerous high-level languages on platforms from 8-bit MCUs to Nvidia GPUs.

Updated on June 25, 2022

Comments

  • Rick
    Rick almost 2 years

    I have an app which is a simplified version of the Spring Bookmark tutorial. In it, Controllers are annotated with @RestController and the app only returns JSON.

    I've added to mine the ability to return HTML via Thymeleaf templates. My templates are being returned, but they don't seem to be processed by Thymeleaf. I'm using spring-boot, and I've spring-boot-starter-thymeleaf to my build.gradle file, but that doesn't seem to be enough.

    For example, here's a simple controller for the root:

    package com.latencyzero.hoa;
    
    import org.springframework.stereotype.Controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.servlet.ModelAndView;
    
    
    
    @Controller
    @RequestMapping("/")
    class
    MainController
    {
        @RequestMapping(method = RequestMethod.GET)
        ModelAndView
        index()
        {
            ModelAndView mav = new ModelAndView("index");
            mav.addObject("version", "0.1");
            return mav;
        }
    }
    

    and src/main/resources/templates/index.html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8"/>
        <title>HOA v${version}</title>
    </head>
    <body>
    <h1>HOA v${version}</h1>
    </body>
    </html>
    

    Results in the following page being rendered:

    enter image description here

    The examples I've found suggest this is all I need to do, but it's not working. Do I need some additional configuration annotations somewhere?

    Thanks.