kingified
kingified

Reputation: 313

Error displaying HTML with a simple thymeleaf Spring boot application

I am trying to display a HTML file with Spring and Thymeleaf but when I hit the controller from the browser, I get error shown as below:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [error], template might not exist or might not be accessible by any of the configured Template Resolvers
    at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    

below is my Controller code..

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thymeLeafAppDemoController;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;


@Controller
public class DemoController {
        
    @GetMapping(value="/show")
    public String stamp(){
        
        
        return "helloworld";
    }
    
}


this is my application.properties file:

server.error.whitelabel.enabled=false

this is my pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dafe</groupId>
<artifactId>thymeLeafAppDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>thymeLeafAppDemo</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>11</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

below is the Main Spring boot class:

package com.thymeLeafAppDemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ThymeLeafAppDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ThymeLeafAppDemoApplication.class, args);
    }

}


the file I am trying to display is a helloworld.html file and this is the path src/main/resources/templates/helloworld.html. The url I used on the browser is http://localhost:8080/show.

This is the content of the file:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <title>Thymeleaf Demo</title>
</head>

<body>

<p th:text="'Time on the server is ' "/>

</body>

</html>

Please help me fix this issue... it is my first time with thymeleaf. Thanks.

Upvotes: 0

Views: 740

Answers (1)

kingified
kingified

Reputation: 313

I finally figured the answer out so I thought to share for the benefit of others in the future. The reason I was having the issue is because Spring Boot could not find my controller class. By default, Spring Boot will component scan the current package of main spring app and all sub-packages.

The package of my main spring app is: com.myThymeLeafDemo

However, my controller is not in a proper sub-package. The controller is current in package: com.myThymeLeafDemoController. However, it should be in a sub-package of com.myThymeLeafDemo

I resolved this by refactoring the package name:

old name: com.myThymeLeafDemoController

new name: com.myThymeLeafDemo.Controller

make note of the "." ... this makes it a subpackage of the main package. Now Spring Boot is be able to find my controller. Once I run my app then it works as desired.

Upvotes: 2

Related Questions