【问题标题】:"Circular view path would dispatch back to the current handler URL." when using Spring Boot and Thymeleaf“圆形视图路径将发送回当前处理程序 URL。”使用 Spring Boot 和 Thymeleaf 时
【发布时间】:2020-12-11 15:33:02
【问题描述】:

当使用 Spring Boot 和 Thymeleaf 时,当尝试访问 /home URL 时,我得到以下信息:

ServletException:循环视图路径 [home]:将再次分派回当前处理程序 URL [/home]。检查您的 ViewResolver 设置! (提示:由于默认视图名称生成,这可能是未指定视图的结果。)

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>

【问题讨论】:

    标签: java spring spring-boot maven thymeleaf


    【解决方案1】:

    用注解@RestController 而不是@Controller 来注解你的控制器类“HomeController”。
    似乎与Circular View path error Spring boot 重复

    【讨论】:

    • 那个不行,改成@RestController后,同样的异常出现。
    • @MaximeEsnol 我创建了与您上面提到的内容相同的 Spring boot 项目,它工作正常。我希望你没有任何其他配置。我在资源文件夹中添加了 application.properties 文件并设置了“spring.thymeleaf.enabled=true”。如果我将其设置为 false,我会得到与您相同的异常。
    • 添加该标志解决了问题!谢谢!
    猜你喜欢
    • 2020-01-08
    • 2017-06-17
    • 2016-04-01
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 2014-09-10
    • 2020-12-25
    • 2020-05-26
    相关资源
    最近更新 更多