【问题标题】:Spring boot cannot find index.html under resoures folderSpring Boot 在资源文件夹下找不到 index.html
【发布时间】:2017-05-01 12:14:59
【问题描述】:

文件夹结构:

我无法访问资源/静态文件夹下的 test.html:

src-> main-> java-> resources -> static -> test.html

http://localhost:8080/test.html(不工作)

但能够到达index.html下:

src-> main-> java-> webapp-> index.html

http://localhost:8080/index.html(工作中)

从 Spring Boot 文档看来,我应该将所有静态内容放在 /src/main/resources/static 下

额外信息:<packaging>war</packaging>

战争文件中的WEB-INF:

知道为什么我无法访问 test.html 吗?

【问题讨论】:

  • test.htmlpublic 移动到 static。如果使用 ThymeLeaf,请将其移至 src/main/resource/templates
  • java 和 maven 标签在这里是否相关?
  • 嗨,我把它移到了静态文件夹,但仍然得到 404。
  • 我遇到了完全相同的问题,除了我得到的是 403 Forbidden,而不是 404。

标签: java maven web-applications spring-boot static-content


【解决方案1】:

您必须添加文件扩展名:“index.html”。 另一种选择(如果你想让它在没有扩展的情况下工作)是 向 pom.xml 添加 thymeleaf 依赖。

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

【讨论】:

    【解决方案2】:

    现在你想将你的资源保存到不在

    的资源文件夹中

    src-&gt; main-&gt; java

    默认情况下,Spring-boot 正在搜索该目录下的 .html 文件:

    src-> main-> resources-> templates
    

    正如我所见,您是说您将自己的放入:

    src-> main-> JAVA-> resources
    

    这是错误的。

    简单:

    将您的控制器、服务、存储库等保存在java 文件夹中,并将您的资源保存在resources 文件夹中。您可以在其中有一个用于 css、字体、img、jsstatic 文件夹和一个您应该保存 htmlstemplates 文件夹。

    【讨论】:

    • 您好,谢谢,但事实并非如此。我把我的java类放在src->main->java下,并使用没有spring-mvc的jersey。我也尝试将我的 .html 文件放在 src-> main -> 资源 -> 模板下,但仍然得到 404。我只能在 webapp 文件夹下访问我的 .html 文件。
    【解决方案3】:

    您需要将内容实际放置在名为 static 的文件夹中。

    层次结构是 src/main/resources/static/index.html

    【讨论】:

    • 嗨,我把它移到了静态文件夹,但仍然得到 404
    猜你喜欢
    • 2017-03-13
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 2019-04-15
    • 2018-03-09
    • 2016-04-16
    相关资源
    最近更新 更多