Spring - 无法解析 MVC“视图”thymeleaf

问题描述 投票:0回答:9

我有一个简单的

HomeController.class

package com.example.tacos;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/")
    public String home() {
        return "home";
    }
}

我还有一个名为

home.html

的模板
<!DOCTYPE HTML>
    <head>
    <title>Getting Started: Serving Web Content</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <p>Hey</p>
    </body>
</html>

无论如何,我在浏览器中收到 404,并且 IDE 告诉我无法解析 MVC“视图”,如您在屏幕上看到的那样

文件夹结构:

浏览器:

spring spring-boot spring-mvc thymeleaf
9个回答
10
投票

嗨,我遇到了同样的问题,我通过删除 Thymeleaf 的版本来修复它,这样 pom 文件就会像这样:

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

6
投票

要解决此问题,您应该将

web app
包的路径添加到 Web 资源目录中。

  1. Alt + Ctrl + Shift + S(“项目结构”窗口打开)
  2. 转到“Facets”选项卡
  3. 然后单击“Web”选项卡
  4. 将新包添加到“Web 资源目录”


2
投票

问题是在 pom.xml 中我有这个:

<dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.0.11.RELEASE</version>
</dependency>

而不是这个:

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

刚开始学习spring,还没有得到足够好的依赖:)


1
投票

我在项目中遇到了同样的问题:

"Cannot resolve MVC view"

其原因是

misspelling
与文件夹的命名。

结构是:

   |   |-- resources
   |   |   |-- template
   |   |   |   |-- customer.html
   |   |   |   |-- customerForm.html
   |   |   |   |-- customers.html
   |   |   |   |-- index.html

我需要使用该名称作为文件夹的

templates
,但我有
template

也许,这对某人也有帮助。


1
投票

我发现我当前的项目是在另一个以前的项目中创建的。

更改项目位置后修复了错误。


0
投票

我在我的 pom.xml 中找到了 gigira 建议的正确条目,但仍然收到错误。

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

我必须修复 在 IntelliJ IDE 中找不到 Maven 插件 并重建。


0
投票

我将tomcat的版本从10.0.20更改为8.5.78并解决了。


0
投票

对于任何使用 Gradle 的人,我的 build.gradle 文件中有这个;

implementation 'org.thymeleaf:thymeleaf:3.1.2.RELEASE'

我已经改成

implementation('org.springframework.boot:spring-boot-starter-thymeleaf:3.0.4')

它对我有用。


0
投票

我之前添加了依赖项

<!-- Thymeleaf -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
        <version>3.1.4</version>
    </dependency>

即使我重新加载 Maven 项目,它也不起作用。

然后我添加了

<version>3.1.4</version>
再次重新加载,成功了!

© www.soinside.com 2019 - 2024. All rights reserved.