"Circular view path would dispatch back to the current handler URL." when using Spring Boot and Thymeleaf

huangapple go评论92阅读模式
英文:

"Circular view path would dispatch back to the current handler URL." when using Spring Boot and Thymeleaf

问题

当在使用 Spring Boot 和 Thymeleaf 时,尝试访问 /home URL 时,我遇到了以下问题:

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

pom.xml:

<!-- 省略 pom.xml 部分 -->

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

// 省略 WatchlistApplication.java 部分

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

// 省略 HomeController.java 部分

main/resources/templates/home.html:

<!-- 省略 home.html 部分 -->
英文:

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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
	&lt;parent&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
		&lt;version&gt;2.3.3.RELEASE&lt;/version&gt;
		&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
	&lt;/parent&gt;
	&lt;groupId&gt;com.demo&lt;/groupId&gt;
	&lt;artifactId&gt;watchlist&lt;/artifactId&gt;
	&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
	&lt;name&gt;watchlist&lt;/name&gt;
	&lt;description&gt;Demo project for Spring Boot&lt;/description&gt;

	&lt;properties&gt;
		&lt;java.version&gt;1.8&lt;/java.version&gt;
	&lt;/properties&gt;

	&lt;dependencies&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
			&lt;scope&gt;test&lt;/scope&gt;
			&lt;exclusions&gt;
				&lt;exclusion&gt;
					&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
					&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
				&lt;/exclusion&gt;
			&lt;/exclusions&gt;
		&lt;/dependency&gt;
	&lt;/dependencies&gt;

	&lt;build&gt;
		&lt;plugins&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
			&lt;/plugin&gt;
		&lt;/plugins&gt;
	&lt;/build&gt;

&lt;/project&gt;

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(&quot;/home&quot;)
    public ModelAndView home() {

        return new ModelAndView(&quot;home&quot;);
    }
}

main/resources/templates/home.html:

&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
&lt;head&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width = device-width, initial-scale = 1, shrink-to-fit = no&quot;&gt;
    &lt;title&gt;Web app&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;p th:text=&quot;&#39;Hello world&#39;&quot;&gt;&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 10

请为您的控制器类"HomeController"添加注解"@RestController",而不是"@Controller"。

看起来与https://stackoverflow.com/questions/36697663/circular-view-path-error-spring-boot重复。

英文:

Annotate you controller class "HomeController" with annotation @RestController instead @Controller.
<br>
Seems duplicate of https://stackoverflow.com/questions/36697663/circular-view-path-error-spring-boot

答案2

得分: 1

@Controller
public class HomeController {

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

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

你应该有 resources/templates/home.html

确保 spring-boot-starter-thymeleaf 已成功下载。


<details>
<summary>英文:</summary>

For springboot 2.6.0, Try **controller**:

```java
@Controller
public class HomeController {

    @GetMapping(&quot;/home&quot;)
    public String home() {

        return &quot;home&quot;; // 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.

huangapple
  • 本文由 发表于 2020年8月22日 21:09:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63536544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定