【问题标题】:spring-boot 404 error in serving .js, .css files to html templates将 .js、.css 文件提供给 html 模板时出现 spring-boot 404 错误
【发布时间】:2016-11-27 23:30:46
【问题描述】:

我正在使用 Spring Boot,并试图托管使用 Angular js 的 HTML 页面。 我的文件夹结构是:

我提到了很多关于 SO 甚至 spring.io 春季启动指南的其他问题,我的理解是我们应该将我们的 html 模板保存在模板文件夹和所有 .css、图像、.js 文件中在静态文件夹中。我也做了同样的事情,但是每当页面加载时,只有 html 被渲染并且浏览器为所有 .css 和 .js 文件获取 404。

我以以下方式在我的 HTML 中包含 .css 和 .js:

<script src="/js/socket.io/socket.io.js"></script>
<script src="/js/moment.min.js"></script>
<script src="/js/demoApp.js"></script>

我尝试了所有组合,例如: src="../static/js/socket.io/socket.io.js" 或 src="js/socket.io/socket.io.js" 但我总是在浏览器中为这些文件收到 404。

我检查了 Spring Boot 日志,上面是这样写的:

2016-07-24 21:08:05 WARN  PageNotFound:1139 - No mapping found for HTTP request with URI [/js/demoApp.js] in DispatcherServlet with name 'dispatcherServlet'
2016-07-24 21:08:05 DEBUG DispatcherServlet:997 - Successfully completed request

我无法理解如何解决这个问题,请帮助!!!

注意:我没有编写视图解析器代码,因为根据我的理解,spring boot 会自动从静态文件夹中获取静态内容。如果我错了,请纠正我。

编辑:添加我的 pom 文件:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
        </dependency>
        <!--Alexa Dependencies -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.directory.studio</groupId>
            <artifactId>org.apache.commons.io</artifactId>
            <version>2.4</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.amazon.alexa</groupId>
            <artifactId>alexa-skills-kit</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.0.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-dynamodb</artifactId>
            <version>1.9.40</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <verbose>true</verbose>
                    <source>1.8</source>
                    <target>1.8</target>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
        </plugins>
    </build>

【问题讨论】:

  • 看起来应该是正确的,因为我的设置是这样的,我不需要参考静态。我正在使用 Freemarker 模板,其中一些模板具有 Angular 并且可以正常工作。你能分享你项目的 POM 文件吗?
  • 是的,编辑完成以显示 pom 文件,@RobBaily 请检查!!
  • 请显示您的 InternalResourceViewResolver(xml 或 java)
  • 这个话题你可能会感兴趣:stackoverflow.com/questions/29953245/…
  • @Georgesvanhoutte 非常感谢,这很有帮助,但正如我回答的那样,只是扩展 WebMvcAutoConfiguration 类有帮助,我不必提供任何视图解析器或创建任何新控制器。

标签: java spring spring-mvc configuration spring-boot


【解决方案1】:

我看到你正在使用百里香,所以尝试像这样访问你的资源:

<script th:src="@{/js/socket.io/socket.io.js}"></script>
<script th:src="@{/js/moment.min.js}"></script>
<script th:src="@{/js/demoApp.js}"></script>

另外,如果这不起作用,您可以添加您的 index.html。

【讨论】:

  • 对不起,我不得不更新这个,我已经删除了 thymleaf,因为我在 UI 中使用了 angularjs。
【解决方案2】:

我发现了问题,一切都正确,但是: 1) 我没有从 WebMvcAutoConfiguration 扩展我的 Application 类。

@SpringBootApplication
@EnableWebMvc
public class HLACUIApplication extends WebMvcAutoConfiguration {

    public static void main(String[] args) {
        SpringApplication.run(HLACUIApplication.class, args);
    }

}

应用程序类应该是这样的。

2) 我不需要使用模板文件夹,因为我使用的是 angularjs 而不是 thymeleaf。我把所有东西都放在了静态文件夹中。

【讨论】:

    【解决方案3】:

    在我使用百里香的情况下,当我在“js”之前跳过“/”时有所帮助。

    示例,而不是

    <script src="/js/demoApp.js"></script>
    

    我放了

    <script src="js/demoApp.js"></script>
    

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题。我认为您的html 代码如下:

      <html xmlns:th="http://www.thymeleaf.org">
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
          <title>Insert title here</title>
      
          <!-- Main Styles -->
          <link rel="stylesheet" href="/css/bar.css" />
          <script src="/js/foo.js"></script>
      
          </head>
          <body>
              <div>Welcome to Foo!</div>
          </body>
          </html>
      

      我通过将type="text/javascript" 添加到&lt;script&gt; 元素解决了这个问题。在Spring Boot 应用程序中需要定义type="text/javascript",否则无法加载。

      您的示例的解决方案如下所示:

          <html xmlns:th="http://www.thymeleaf.org">
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
          <title>Insert title here</title>
      
          <!-- Main Styles -->
          <link rel="stylesheet" href="/css/bar.css" />
          <script type="text/javascript" src="/js/foo.js"></script>
      
          </head>
          <body>
              <div>Welcome to Foo!</div>
          </body>
          </html>
      

      【讨论】:

        【解决方案5】:

        如果您使用的是Eclipse IDE,请右键单击项目属性/项目方面

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-02-03
          • 1970-01-01
          • 2021-11-03
          • 1970-01-01
          • 2017-08-26
          • 2021-05-04
          • 1970-01-01
          • 2018-11-15
          相关资源
          最近更新 更多