【问题标题】:Spring boot with thymeleaf could not resolve view with name带有百里香叶的 Spring Boot 无法解析带有名称的视图
【发布时间】:2019-10-10 11:44:36
【问题描述】:

我正在使用 thymeleaf 设置一个 Spring Boot 项目,但我的视图没有显示并且在浏览器中出现以下错误。

白标错误页面
此应用程序没有显式映射 /error,因此您将其视为后备。

2019 年 5 月 24 日星期五 00:44:22 GST
出现意外错误(类型=未找到,状态=404)。
没有可用的消息

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

控制器:

@Controller
public class MainController {

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

application.properties:

spring.thymeleaf.cache=false
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

spring.application.name=Bootstrap Spring Boot

我在我的 html 文件中添加了 thymeleaf 功能,但是当它不起作用时,我只是把它变成了简单的 html 文件。我的 home.html 是:

<html>
<head>
<title>Home Page</title>
</head>
<body>
    <h1>Hello !</h1>
    <p>
        Welcome to Our App</span>
    </p>
</body>
</html>

这是一个非常简单和基本的控制器。我在控制台中没有看到任何异常堆栈跟踪。

【问题讨论】:

  • return "index";。并且还要添加 并且还要添加&lt;html xmlns:th="http://www.thymeleaf.org"&gt;
  • @want2learn,关于"index" 抱歉,这是拼写错误。我已经更新了这个问题。我已经添加了推荐的标签,但是没有用。

标签: spring spring-boot spring-mvc thymeleaf


【解决方案1】:

我有一个类似的问题。我通过将 home.html 页面重命名为 index.html 来解决它,spring 似乎会自动解决它。像魔术一样工作

【讨论】:

    【解决方案2】:

    经过几天的努力,我通过将 Spring Boot 版本从 2.1.5 降级到

    解决了这个问题

    &lt;version&gt;2.0.6.RELEASE&lt;/version&gt;

    【讨论】:

      【解决方案3】:

      当您看到此错误时,可能的原因有很多,请尝试以下方法:

      • 确保您的文件夹结构正确。当不遵循 maven 文件夹结构时,spring 可能难以发现端点等。
      • 确保将主 application.java 保留在其他类之上的根包,因为 Spring Boot 注释只扫描主类包之下的类。

      例如:

      com
         +- APP
               +- MainApplication.java  <--- your main class should be here, above your other classes
               |
               +- model
               |   +- model.java
               +- controller
                   +- Controller.java
      
      • 确保您的资源放置在正确的目录中。

      默认情况下,Spring Boot 从名为 /static (or /public or /resources or /META-INF/resources) 在 类路径或来自 ServletContext 的根目录。

      当您将这些模板引擎之一与默认设置一起使用时 配置,您的模板会自动从 src/main/resources/templates.

      官方文档:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html

      由于您已经在application.yml 中提供了thymeleaf 配置,您可以将您的thymeleaf 模板放在资源文件夹的/templates 文件夹中,spring 应该会使用它。

      【讨论】:

      • 我已经仔细检查过,一切都准备好了。有趣的是,我刚刚将我的 Spring Boot 版本降级为&lt;version&gt;1.3.6.RELEASE&lt;/version&gt;,它就开始工作了。也许有一些兼容性问题。
      • 哦,太好了,需要通过更改列表找出导致问题的原因。
      猜你喜欢
      • 2020-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 2017-10-21
      • 2019-03-30
      • 2018-04-02
      • 2013-11-26
      相关资源
      最近更新 更多