圆形视图路径错误 弹簧启动

2022-09-01 00:15:03

我对春天很陌生。我正在尝试使用Spring Boot构建一个MVC应用程序,该应用程序显示了产品列表。但是我得到以下错误:

javax.servlet.ServletException:循环视图路径 [products]: 将再次调度回当前处理程序 URL [/products]。检查您的视图解析器设置!(提示:这可能是由于默认视图名称生成而导致的未指定视图的结果。

这是控制器:

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>

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


答案 1

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

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

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

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

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

第2步:运行“渐变包装器”

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

第4步:现在使用日食运行

第5步:享受!


答案 2

你也可以在这里,因为:

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

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


推荐