【问题标题】:Spring Boot MVC html file paths not workingSpring Boot MVC html文件路径不起作用
【发布时间】:2017-03-03 04:20:33
【问题描述】:

刚开始使用 Spring Boot MVC。一直在挠头试图整理我的文件夹。

我的 index.html、CSS 和图像文件位于以下文件夹中:

  • /resources/static,
  • /resources/static/css
  • /resources/static/images 分别。

一切正常。我在/resources/templates/greeting.html 中有一个 HTML 文件,我的控制器类可以正确访问它。但是,我创建了一些子文件夹 /resources/templates/management/*.html,但我的 /resource/templates/greeting.html 无法引用这些文件夹,并且出现 404 错误。

如果我将文件从/management 中取出并放入/resources,它们就可以正常访问。这是我的index.html 代码:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head> 
    <title>AVI Administrator</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="\css\indexstyles.css" />
</head>
<body>
<div id="pagewrap">

    <div class="header">
        <img src="\images\avi.png" alt="AVI" style="width:90px;height:50px;" >
        <h1> Edge Administrative Dashboard </h1>
    </div>

    <section id="content">
        <center>
            <img src="\images\wrench_1024.png" alt="Administration" style="width:200px;height:200px;" >
            <h2>Account Management</h2>
        </center>
        <br>
        <a href="\management\addadministrator.html" style="text-decoration: none" >&gt;&nbsp;&nbsp; Add/Edit System Administrator Account<p/></a>
        <a href="\management\addclient.html" style="text-decoration: none" >&gt;&nbsp;&nbsp; Add/Edit Tenant Client Account<p/></a>
        <a href="\management\addtenant.html" style="text-decoration: none" >&gt;&nbsp;&nbsp; Add/Edit Tenant Account<p/></a>
        <a href="\management\addtenantedgedevice.html" style="text-decoration: none" >&gt;&nbsp;&nbsp; Add/Edit Edge Device<p/></a>
        <a href="\management\addtenantsubscription.html" style="text-decoration: none" >&gt;&nbsp;&nbsp; Add/Edit Tenant Subscription<p/></a>
    </section>

    <footer>

    </footer>
</div>
</body>
</html>

有人知道发生了什么吗?我正在创建一个可执行的 JAR 并阅读了 WEB-INF 的一些文件限制。 Spring Boot 是否要求所有 HTML 文件都在 /resources 文件夹中?

我似乎没有违反 spring-boot MVC jar 问题。有没有更好的方法来组织文件夹以更好地组织文件?欢迎提出建议。

【问题讨论】:

  • 您可能因为/resources/template/management/*.html 而不是/resources/templates/management/*.html 而收到404 错误?只是在template(s) 中打错字。有子文件夹是可以的,你可以在你的控制器中管理它return "subfolder/pageName";
  • 抱歉错字。当我输入 stackoverflow 时,它们是我的错误。实际来源是正确的。

标签: spring-mvc spring-boot


【解决方案1】:

你的参考是错误的。

改成:

<a href="management/addadministrator.html" style="text-decoration: none" >&gt;&nbsp;&nbsp; Add/Edit System Administrator Account<p/></a>
<a href="management/addclient.html" style="text-decoration: none" >&gt;&nbsp;&nbsp; Add/Edit Tenant Client Account<p/></a>

【讨论】:

  • 试过但没用。我也试过
  • 只是为了重新迭代,我使用的是 spring-boot MVC。文件组织与大多数 Web 应用程序略有不同。我当前不起作用的文件组织是/resources/templates/management/addclient.html。我将管理文件夹移动到静态文件夹中并且它工作。工作组织是/resources/static/management/addclient.html。我猜 spring-boot MVC 想要静态文件中的静态内容和模板文件夹中的控制器引用文件。这有意义吗?还有其他意见吗?
【解决方案2】:

好的,经过大量阅读,我终于找到了问题所在。引起很多问题的第一件事是使用 Spring Boot MVC 并创建可执行 Jar。当您创建“Spring Boot Starter Project”时,会创建 2 个路径,

resources
   |
   --> static
   |
   --> templates

默认情况下,当使用可执行 jar 时,所有静态内容——css、js、字体——都放在静态文件夹中。如果使用 index.html,它也会被放置在静态文件中。同样,默认情况下,所有其他 html 文件都放在模板文件夹中。通过遵循这些默认标准,它可以正常工作。一个真正让我浪费很多精力的问题是创建一个可执行的 .jar 并使用典型的 MVC 文件结构 webapp --> WEB-INF。可执行 jar 忽略 webapp 文件结构。另一个小惊喜是,当创建可执行的 .jar 时,.jsp 真的不能很好地与 Spring Boot MVC 配合使用。 WAR 部署几乎消除了所有这些问题。 Spring Boot 在使用可执行的 .jars 时建议将 thymeleaf 或 html 用于视图页面。

如果我错过了其他任何事情?

【讨论】:

  • 我想你越来越近了。 Spring Boot 支持各种模板引擎(Freemarker、thymeleaf、Mustache、Groovy),如果您将 .war 部署到标准 Web 容器,.jsp 将可以正常工作。我还从您的代码中注意到您正在静态引用 html 文件。 hrefs 应该指向控制器请求映射。控制器应该调用您的视图/html 文件 (MVC)。
  • Olantobi...感谢您指出静态 href。我修复了 href 并指向控制器请求映射而不是 .jsp。我的 spring boot mvc 工作正常。最后一个问题是可执行 jars。我刚刚在 gradle 构建中创建了一个可执行的 .war,一切正常。您在命令行中执行 .war 为; java -jar myjarname.war
猜你喜欢
  • 2019-10-30
  • 2021-11-10
  • 2018-07-04
  • 1970-01-01
  • 2021-06-07
  • 2018-05-26
  • 2015-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多