【问题标题】:Spring - Cannot resolve MVC "view" thymeleafSpring - 无法解析 MVC“视图”百里香叶
【发布时间】:2020-05-17 00:23:36
【问题描述】:

我得到了一个简单的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


    【解决方案1】:

    您好,我遇到了同样的问题,我通过删除 Thymeleaf 的版本来修复它,所以 pom 文件将如下所示:

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

    【讨论】:

      【解决方案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,还没有很好地理解依赖关系:)

      【讨论】:

        【解决方案3】:

        要解决此问题,您应该将 web app 包的路径添加到 Web 资源目录中。

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

        【讨论】:

          【解决方案4】:

          我的 pom.xml 中有 gumira 建议的正确条目,但我仍然收到错误消息。

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

          我不得不修复 Maven Plugin not found in IntelliJ IDE 并重建。

          【讨论】:

            猜你喜欢
            • 2019-10-10
            • 1970-01-01
            • 1970-01-01
            • 2016-07-02
            • 2020-11-28
            • 2016-08-14
            • 2015-11-29
            • 1970-01-01
            相关资源
            最近更新 更多