【发布时间】:2017-01-04 08:41:57
【问题描述】:
我是 Spring Boot 的新手,正在努力将一个简单的 HTML Web 应用程序 (AngularJS) 部署到 Tomcat 8。这个 Web 应用程序只提供一些 HTML/CSS/JS 内容,没有对后端的 REST 调用。它已经使用 Webpack 进行了“编译”——这会生成 JS/CSS 包和一个 index.html 文件,该文件通过<script> / <link> 标签指向它们——并且已经在 ExpressJS 和带有嵌入式 tomcat 的 Spring Boot 上进行了测试,并且可以作为预期的。但是沿着独立 WAR 的路径并部署到 Tomcat 8 似乎无法正常工作。
对于 Spring Boot 项目,我已将所有 HTML/CSS/JS 文件包含在 src/main/resources/public 文件夹(无子文件夹)中,并且还配置了 pom.xml 如下(基本配置):
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.example.my-app</groupId>
<artifactId>my-app</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<name>my-app</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
“主”类:
package com.example.my.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
@SpringBootApplication
public class MyAppApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyAppApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyAppApplication.class, args);
}
}
以下是我用来进行 Tomcat 部署的步骤:
-
mvn clean package生成 WAR - 将 WAR 复制到路径/to/tomcat8/webapp
- 启动 Tomcat
- http://localhost:8080/my-app => 自动加载 index.html
不幸的是,我看到的只是 404 错误,因为它找不到一些 JS/CSS 文件。有没有我遗漏的配置?
更新: 这是 index.html 文件(通过 Webpack 自动生成):
<!doctype html>
<html>
<head>
<base href="/">
<meta charset="utf-8">
<title>My App</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="icon" type="image/png" href="./assets/images/favicon.ico" />
<link href="/main-aa49893f83cc830596563d81f09a9611.css" rel="stylesheet"><link href="/main-5949f1f257a55a77e48bc4ab62fbc99a.css" rel="stylesheet"></head>
<body ng-app="myapp">
<ui-view></ui-view>
<script type="text/javascript" src="vendor-353ddb48f4ffe6546d59.js"></script><script type="text/javascript" src="app-353ddb48f4ffe6546d59.js"></script></body>
</html>
以下是我在访问 localhost:8080/my-app/index.html 时在 Chrome Web Inspector 中看到的错误:
http://localhost:8080/main-5949f1f257a55a77e48bc4ab62fbc99a.css Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/main-aa49893f83cc830596563d81f09a9611.css Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/vendor-4ba9083ed9802279c207.js Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/app-4ba9083ed9802279c207.js Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/vendor-4ba9083ed9802279c207.js Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/app-4ba9083ed9802279c207.js Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/main-aa49893f83cc830596563d81f09a9611.css Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/main-5949f1f257a55a77e48bc4ab62fbc99a.css Failed to load resource: the server responded with a status of 404 (Not Found)
还有一件事我忘了提。当我使用mvn clean package 生成战争时,src/main/resources/public 下的所有文件都放在WEB-INF/classes/resource 子文件夹中。根据研究,这些文件不是公开可见的(即,如果我尝试访问 localhost:8080/my-app/foo.css,它会给出 404)。这就是为什么 index.html 文件无法“看到”它所依赖的 JS/CSS 文件的原因吗?
【问题讨论】:
-
提供更多关于完整 404 错误是什么的上下文或包含您的 index.html 页面以便人们了解资源文件的加载方式会有所帮助。
标签: angularjs spring spring-mvc tomcat spring-boot