5

When using Spring Boot and Thymeleaf, when trying to accessing the /home URL I am getting the following:

ServletException: Circular view path [home]: would dispatch back to the current handler URL [/home] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

pom.xml:

<?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.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>watchlist</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>watchlist</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</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-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>

</project>

main/java/com.demo.WatchlistApplication.java:

package com.demo;

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

@SpringBootApplication
public class WatchlistApplication {

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

}

main/java/com.demo.controller.HomeController.java:

package com.demo.controller;

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

@Controller
public class HomeController {

    @GetMapping("/home")
    public ModelAndView home() {

        return new ModelAndView("home");
    }
}

main/resources/templates/home.html:

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta name="viewport" content="width = device-width, initial-scale = 1, shrink-to-fit = no">
    <title>Web app</title>
</head>

<body>

<p th:text="'Hello world'"></p>

</body>
</html>
Maxime Esnol
  • 51
  • 1
  • 3
  • 3

2 Answers2

10

Annotate you controller class "HomeController" with annotation @RestController instead @Controller.
Seems duplicate of Circular View path error Spring boot

nilesh_101
  • 307
  • 3
  • 10
  • That did not work, after changing it to @RestController, the same exception occurs. – Maxime Esnol Aug 22 '20 at 13:27
  • @MaximeEsnol I created Spring boot project with same content you mentioned above, its working fine. I hope you do not have any other configuration. I added application.properties file in resource folder and set "spring.thymeleaf.enabled=true". If I set it to false I get same exception as yours. – nilesh_101 Aug 22 '20 at 13:47
  • Adding that flag fixed the problem! Thank you! – Maxime Esnol Aug 22 '20 at 13:56
  • This has worked for me! – havryliuk May 25 '23 at 12:49
1

For springboot 2.6.0, Try controller:

@Controller
public class HomeController {

    @GetMapping("/home")
    public String home() {

        return "home"; // be same as the template file name (without suffix)
    }
}

You should have resources/templates/home.html.

Make sure the spring-boot-starter-thymeleaf has been downloaded successfully.

Nick Dong
  • 3,638
  • 8
  • 47
  • 84