Java 圆形视图路径错误 Spring boot

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36697663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:34:17  来源:igfitidea点击:

Circular View path error Spring boot

javaspringspring-mvcspring-boot

提问by sohan

I am very new to Spring. I am trying to build a MVC application using Spring Boot which shows a list of products. But i am getting the below error:

我对春天很陌生。我正在尝试使用显示产品列表的 Spring Boot 构建 MVC 应用程序。但我收到以下错误:

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

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

Here is controller:

这是控制器:

package com.springframeworkguru.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.springframeworkguru.services.ProductService;


    @Controller
    public class ProductController {

        private ProductService productService;

        @Autowired
        public void setProductService(ProductService productService) {
            this.productService = productService;
        }

        @RequestMapping("/products")
        public String listProducts(Model model){

            model.addAttribute("products", productService.listAllProducts());

            return "products";
        }

    }

This is the main class:

这是主类:

package com.springframeworkguru;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

import com.springframeworkguru.controllers.ProductController;

@SpringBootApplication
public class SpringmvcApplication extends SpringBootServletInitializer{

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

and products.html:

products.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Spring Core Online Tutorial - List Products</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

    <link href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.4/css/bootstrap.min.css"
          th:href="@{/webjars/bootstrap/3.3.5/css/bootstrap.min.css}"
          rel="stylesheet" media="screen"/>

    <script src="http://cdn.jsdelivr.net/webjars/jquery/2.1.4/jquery.min.js"
            th:src="@{/webjars/jquery/2.1.4/jquery.min.js}"></script>

    <link href="../css/spring-core.css"
          th:href="@{css/spring-core.css}" rel="stylesheet" media="screen"/>
</head>
<body>
<div class="container">
    <div th:if="${not #lists.isEmpty(products)}">
        <h2>Product List</h2>
        <table class="table table-striped">
            <tr>
                <th>Id</th>
                <th>Description</th>
                <th>Price</th>
                <th>Image URL</th>
                <th>List</th>
            </tr>
            <tr th:each="product : ${products}">
                <td th:text="${product.id}"></td>
                <td th:text="${product.description}"></td>
                <td th:text="${product.price}"></td>
                <td th:text="${product.imageUrl}"></td>
                <td><a th:href="${'/product/' + product.id}">View</a> </td>
            </tr>
        </table>
    </div>
</div>

</body>
</html>

The products.htmlis in /staticfolder. Also, I am using Eclipse Kepler.

products.html是在/static文件夹中。另外,我正在使用 Eclipse Kepler。

回答by Ali Dehghani

The products.html is /static folder

products.html 是 /static 文件夹

By default, Spring Boot will look for Thymeleaf templates in templatesdirectory on the classpath. So move your products.htmlto src/main/resources/templatesdirectory. You can read more about template engines and Spring Boot on the Spring Boot Documentation:

默认情况下,Spring Boot 将templates在类路径上的目录中查找 Thymeleaf 模板。所以移动你products.htmlsrc/main/resources/templates目录。您可以在Spring Boot 文档中阅读有关模板引擎和 Spring Boot 的更多信息:

When you're using thymeleaf templating engine with the default configuration, your templates will be picked up automatically from src/main/resources/templates

当您使用具有默认配置的 thymeleaf 模板引擎时,您的模板将自动从 src/main/resources/templates

Also, staticdirectory is where you should put your Static Contents, not your templates.

此外,static目录是您应该放置静态内容的地方,而不是您的模板。

回答by user18853

Adding spring-boot-starter-thymeleafdependency solved the problem.

添加spring-boot-starter-thymeleaf依赖解决了这个问题。

So add this to your pom.xml file:

因此,将其添加到您的 pom.xml 文件中:

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

Update: If you are working with Eclipse and you are using Gradle, this might not work. The reason is if you haven't imported the project as 'gradle project' Eclipse wont detect thymeleaf. So here's the solution:

更新:如果您使用 Eclipse 并且使用 Gradle,这可能不起作用。原因是如果您没有将项目作为“gradle 项目”导入,Eclipse 将不会检测到百里香叶。所以这是解决方案:

Step1 : Run "gradle eclipse" on command line.

步骤 1:在命令行上运行“gradle eclipse”。

Step2 : Run "gradle wrapper"

第2步:运行“gradle wrapper”

Step3 : In eclipse import as gradle project (before this remove the already imported project)

Step3:在eclipse中导入为gradle项目(在此之前删除已经导入的项目)

Step4 : Now run using eclipse

Step4:现在使用eclipse运行

Step5 : Enjoy!

第5步:享受!

回答by Mehraj Malik

Add the following dependency in pom.xml

在中添加以下依赖项 pom.xml

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>1.4.0.RELEASE</version>
</dependency>

The latest version could be found on mvnrepository

最新版本可以在mvnrepository找到

回答by Nag Arvind Gudiseva

Rename "product.ftl" to "products.ftl".

将“product.ftl”重命名为“product s.ftl”。

回答by S.Dayneko

You can also be here because:

你也可以在这里,因为:

  1. You forgot to put the @RestControllerof your rest controller over the class

  2. You set @Controllerinstead of @RestController

  1. 你忘了把你的休息控制器的@RestController放在类上

  2. 您设置@Controller而不是@RestController

回答by Adam Silenko

Problems can be caused by using an embedded servlet container (embedded tomcat). @mirmdasif answer

使用嵌入式 servlet 容器(嵌入式 tomcat)可能会导致问题。 @mirmdasif 回答

To resolve this use external tomcat server.

要解决此问题,请使用外部 tomcat 服务器。

Configure tomcat server in STS/Eclipse:
1. from top menu: Window > Show View > Servers
2. in servers tab window context menu: New > Server
3. do project config to deploy WAR file to Tomcat.
4. run project as Spring Boot App

在 STS/Eclipse 中配置 tomcat 服务器:
1. 从顶部菜单:Window > Show View > Servers
2. 在服务器选项卡窗口上下文菜单中:New > Server
3. 进行项目配置以将 WAR 文件部署到 Tomcat
4.运行项目Spring Boot App

deploy WAR file to Tomcat
Main class should exteneds SpringBootServletInitializer and override SpringApplicationBuilder method...

将 WAR 文件部署到 Tomcat
主类应该扩展 SpringBootServletInitializer 并覆盖 SpringApplicationBuilder 方法...

package package_with_your_main_class;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class YourStartWebApplication extends SpringBootServletInitializer {

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(YourStartWebApplication.class);
    }
}

pom.xml should contains

pom.xml 应该包含

<!-- Parent pom providing dependency and plugin management for applications -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <!-- this version works with tomcat 8.5, change to newest if you are using newer tomcat -->
    <version>2.0.9.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <java.version>1.8</java.version>
    <!-- The main class to start by executing java -jar -->
    <start-class>package_with_your_main_class.SpringBootWebApplication</start-class>
</properties>

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

    <!-- templating language -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId> 
    </dependency>

    <!-- marked the embedded servlet container as provided -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

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

<packaging>war</packaging>

回答by kamau wairegi

Well I had the same problem using SpringBoot and all I did was replacing @Controller with @RestController and it worked fine.

好吧,我在使用 SpringBoot 时遇到了同样的问题,我所做的只是用 @RestController 替换 @Controller 并且它运行良好。

回答by SAAD BELEFQIH

make sure you have enabled thymeleaf with spring in application.properties :

确保您在 application.properties 中启用了带有 spring 的 thymeleaf :

spring.thymeleaf.enabled=true

spring.thymeleaf.enabled=true