圆形视图路径错误Spring启动

soh*_*han 33 java spring spring-mvc spring-boot

我对Spring很新.我正在尝试使用Spring Boot构建一个MVC应用程序,它显示了一个产品列表.但我收到以下错误:

javax.servlet.ServletException:循环视图路径[products]:将再次调度回当前处理程序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";
        }

    }
Run Code Online (Sandbox Code Playgroud)

这是主要类:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

并且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>
Run Code Online (Sandbox Code Playgroud)

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

use*_*853 90

添加spring-boot-starter-thymeleaf依赖项解决了问题.

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

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

  • 为什么这解决了这个问题? (12认同)
  • 哇!这确实解决了我的问题。错过依赖项并收到完全不同的错误消息似乎很愚蠢。错误信息也非常具有欺骗性。 (4认同)

Ali*_*ani 14

products.html是/ static文件夹

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

当您使用默认配置的百万美元模板引擎时,您的模板将自动从中获取 src/main/resources/templates

此外,static您可以在目录中放置静态内容,而不是模板.

  • 你添加了'spring-boot-starter-thymeleaf`依赖吗? (14认同)

Meh*_*lik 14

添加以下依赖项 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>
Run Code Online (Sandbox Code Playgroud)

可以在mvnrepository上找到最新版本

  • 当您为 Thymeleaf 指定版本时,请注意不要覆盖 SpringBoot 的托管版本!这有时会导致问题。 (2认同)

kam*_*egi 7

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


S.D*_*eko 6

您之所以在这里是因为您忘记将rest控制器的@RestController放在该类上了:)

  • +^ 谢谢!这成功了。我已经添加了“@Controller”,但这不起作用,但是添加“@RestController”使循环依赖问题消失了! (4认同)
  • 感谢您的提示,对我来说这是一个@ResponseBody。最好看一下这些注释。 (3认同)
  • @RestController = @Controller + @ResponseBody。因此,如果您放置 @Controller,简而言之,则假定您将使用演示文稿(jsp、html、thymeleaf),并且应该为您配置它。该错误是由于缺少演示文稿设置而发生的。 (3认同)