java - 循环 View 路径错误 Spring boot

标签 java spring spring-mvc spring-boot

我对 Spring 很陌生。我正在尝试使用显示产品列表的 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.)

这里是 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";
        }

    }

这是主类:

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。

最佳答案

添加 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 运行

第五步:享受吧!

关于java - 循环 View 路径错误 Spring boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36697663/

相关文章:

spring - 如何在 Spring 4 中进行基于关系数据库的 HTTP session 持久性?

java - 无法为 spring security 提供自定义身份验证提供程序

java - 在 Eclipse 4.2 中验证 XML 和 XSD - 缺少选项?

Spring security + i18n = 如何让它一起工作?

java - 从列表中删除 java 类

java - 如何使用 Spring/AJAX 基于另一种方法使 Spring 中的 session 无效

java - Spring Mongodb - 无法为 java.time.Period 编写自定义转换器

java - Spring MVC 如何禁止数据绑定(bind)到 ModelAttribute?

java - 检查子字符串是否存在于java中的字符串数组列表中

java - 获取 "parse exception"