【问题标题】:Circular View path error Spring boot循环视图路径错误 Spring boot
【发布时间】:2016-08-10 09:48:09
【问题描述】:

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

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

这里是控制器:

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";
        }

    }

这是主类:

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);
    }
}

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>

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

【问题讨论】:

    标签: java spring spring-mvc spring-boot


    【解决方案1】:

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

    所以把它添加到你的 pom.xml 文件中:

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

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

    Step1 : 在命令行运行“gradle eclipse”。

    第二步:运行“gradle wrapper”

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

    Step4 : 现在使用 eclipse 运行

    第五步:享受吧!

    【讨论】:

    • 哇!这确实解决了我的问题。错过依赖并得到完全不同的错误消息似乎很愚蠢。错误信息也很有欺骗性。
    • 这对我有用。但是,如果使用 IntelliJ IDEA 和 maven,那么仅将此依赖项添加到您的 pom.xml 可能还不够。就我而言,我还需要转到:查看 > 工具窗口 > Maven,然后单击“重新加载所有 Maven 项目”。只有在那之后我的项目才找到 spring-boot-starter-thymeleaf 然后运行时“圆形视图路径...”错误得到解决。
    【解决方案2】:

    你也可以在这里,因为:

    1. 您忘记将休息控制器的 @RestController 放在类上

    2. 你设置了 @Controller 而不是 @RestController

    【讨论】:

    • +^ 谢谢!这成功了。我放了@Controller,但没有用,但添加@RestController 使循环依赖问题消失了!
    • 感谢您的提示,对我来说这是一个@ResponseBody。最好注意这些注释。
    • 这对我有用。正如@robot_alien 所说,我也使用@Controller 而不是@RestController
    • @RestController = @Controller + @ResponseBody。所以如果你放@Controller,简而言之,假设你将使用演示文稿(jsp,html,thymeleaf)并且应该为你配置它。由于缺少演示文稿的设置而发生错误。
    • @S.Dayneko 我刚刚遇到了同样的情况,将@Controller 替换为@RestController 并且效果很好,然后看到你的评论,这真的是信息丰富的评论。我知道我对这个讨论有点晚了。但是,请您告诉我@RestController 是否仍然可以与thymeleaf 一起正常工作,因为我正在遵循指南并且他们正在使用@Controller,尤其是我不想放弃template 的自动映射功能thymeleaf 提供的目录。再次抱歉,我来晚了。
    【解决方案3】:

    products.html 是 /static 文件夹

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

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

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

    【讨论】:

    • 我已经做到了。但同样的错误 - javax.servlet.ServletException: Circular view path [products]: 将再次调度回当前处理程序 URL [/products]。检查您的 ViewResolver 设置! (提示:由于默认视图名称生成,这可能是未指定视图的结果。)。 2016-04-19 00:01:35.129 错误 3200 --- [nio-8080-exec-5] oaccC[.[.[/].[dispatcherServlet]: Servlet.service() for servlet dispatcherServlet 抛出异常
    • 你添加了spring-boot-starter-thymeleaf 依赖吗?
    • 非常感谢。添加 spring-boot-starter-thymeleaf、spring-boot-starter-web 依赖后,它工作了。
    • 谢谢,添加 spring-boot-starter-thymeleaf 和 spring-boot-starter-web 后它也有效。
    【解决方案4】:

    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>
    

    最新版本可以在mvnrepository找到

    【讨论】:

    • 当您为 Thymeleaf 指定版本时,请注意不要覆盖 SpringBoot 的托管版本!这有时会导致问题。
    【解决方案5】:

    嗯,我在使用 SpringBoot 时遇到了同样的问题,我所做的只是替换 @Controller 和 @RestController 效果很好。

    【讨论】:

      【解决方案6】:

      @Controller 转换为@RestController,这将解决循环路径问题。

      【讨论】:

        【解决方案7】:

        默认情况下,Spring Boot 使用 InternalResourceView 类作为视图解析器。如果@GetMapping 值与视图名称相同,则请求失败并出现循环视图路径错误。

        因此,一种解决方案是不要对 URL 路径和视图名称使用相同的名称。

        如果我们选择 Thymeleaf 处理器,则错误不存在。

        我有一个使用 Freemarker 的示例,并且出现了圆形视图路径错误(Spring Boot 2.2.4)。我不得不重命名 URL 路径。

        【讨论】:

          【解决方案8】:

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

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

          在 STS/Eclipse 中配置 tomcat 服务器:
          1. 从顶部菜单:Window &gt; Show View &gt; Servers
          2. 在服务器选项卡窗口上下文菜单中:New &gt; Server
          3. 对deploy WAR file to Tomcat进行项目配置。
          4.以Spring Boot App运行项目

          将 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 应该包含

          <!-- 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>
          

          【讨论】:

            【解决方案9】:

            确保您已启用 thymeleaf with spring 在 application.properties 中:

            spring.thymeleaf.enabled=true

            【讨论】:

              【解决方案10】:

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

              【讨论】:

                【解决方案11】:

                在我的例子中,spring boot 2 和 jdk 11 中的圆形视图路径是通过重定向到 index.html 来修复的:

                    @Bean
                    public WebMvcConfigurer corsConfigurer() {
                        return new WebMvcConfigurer() {
                            }
                            @Override
                            public void addViewControllers(ViewControllerRegistry registry) {
                                registry.addViewController("/").setViewName("redirect:/index.html");
                            }
                        };
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-03-29
                  • 2019-02-06
                  • 1970-01-01
                  • 2020-03-22
                  • 1970-01-01
                  • 2016-03-13
                  相关资源
                  最近更新 更多