我正在努力解决一个问题,即 spring-boot 无法在 8080 上提供前端文件(通过 Angular 创建)。我运行了 ng build
将 .ts ng 组件文件转换为 .js 并在 outputPath:/resource/static 文件夹中生成(我在 angular.json 文件中设置了 outputPath)。
创建了一个 Controller 来提供来自 resources/static 的内容。
@Controller
public class AppController {
@GetMapping("/")
public String index() {
return "index";
}
}
主类(src/mainjava/webapp/app):
@Configuration
@EnableAutoConfiguration
@ComponentScan({"webapp"})
public class Application extends WebMvcAutoConfiguration {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.run(args);
}
}
冉ng build --base-href .
生成如下图所示的文件。
index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome app</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
<script type="text/javascript" src="runtime.js"></script><script type="text/javascript" src="polyfills.js"></script><script type="text/javascript" src="styles.js"></script><script type="text/javascript" src="vendor.js"></script><script type="text/javascript" src="main.js"></script></body>
</html>
注意:这是一个gradle项目。前端代码位于:src/main/java/webapp/frontend dir
/resources 中的Application.yml
server:
servlet:
context-path: /
port: 8080
在 tomcat 启动时:
Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-13 12:45:34.372 INFO 96027 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-13 12:45:34.384 INFO 96027 --- [ main] .m.m.a.ExceptionHandlerExceptionResolver : Detected @ExceptionHandler methods in exceptionHandle
2018-09-13 12:45:34.405 INFO 96027 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html]
2018-09-13 12:45:34.566 INFO 96027 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-09-13 12:45:34.568 INFO 96027 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource' has been autodetected for JMX exposure
2018-09-13 12:45:34.575 INFO 96027 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
2018-09-13 12:45:34.620 INFO 96027 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
我什至尝试在/resources 下添加 html 文件,以查看 tomcat 是否提供此文件夹中的任何内容。我也无法访问 page1.html,因此不确定 Angular/ng 内容有何不同,以至于在 tomcat 启动并运行时无法提供服务。
我需要任何其他配置才能让 spring-boot 服务角度前端代码吗?如有任何建议,我们将不胜感激。
@DavidT 回答您的问题:
是的 构建.gradle:
compile('org.springframework.boot:spring-boot-starter-web:2.0.2.RELEASE')
此处的静态内容: MYPROJECT/MYPROJECT-web/src/main/resources/static/
由于 spring-boot 没有从路径中选择角度静态内容,所以我 引用了建议添加 Controller 的在线资源 专门为它服务。 [我有 3 个可用组件 - 登录:http://localhost:4200/ ,首页:http://localhost:4200/home ,销售:http://localhost:4200/sale 。但 要访问tomcat端口,我想将文件放在静态下;休息 Angular 将负责路由。]
运行时
ng build
从 frotnend 目录,在 frontend/src 目录下生成角度文件。我可以手动复制、手动粘贴以放入静态文件夹或更改outputPath
指向 angular.json 中的 resources/static ,它直接将其放在运行 ng build cmd 时。精确的 上述资源/静态屏幕截图中的文件。我没有使用过javascript控制台,而是使用浏览器打开index.html IntellijIdea 的选项,在网页上检查我看到错误 -
Refused to execute script from '' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
替换 <script type to "text/html"
从检查中删除了这些错误,但 http://localhost:8080/ 上仍然没有 UI .
奇怪的是,当我将演示项目中的相同 index.html 文件粘贴到静态文件夹中的我的项目时,当浏览到 http://localhost:8080 时,它不会显示任何内容。 。但注意到当从 IntellijIdea 中的浏览器选项打开此文件时(浏览器中的此 URI: http://localhost:63342/MY-PROJ/MY-PROJ-WEB-main/static/index.html ,然后它显示内容
是 <base href="/">
问题或脚本标签?
最佳答案
成功的步骤并不复杂,如上面的链接所示:
- 前往
https://start.spring.io/
- 选择了一个 Gradle 项目
- 添加了 Web 依赖项
- 下载并取消存档结果(未更改
演示
中的名称) - 创建:demo/src/main/resources/static/index.html,包含以下内容:
<html><body>YOU ARE IN!</body></html>
- 在
demo
目录中发出以下命令来运行您的应用程序:./gradlew bootRun
- 打开浏览器并访问
http://localhost:8080/
您现在有证据表明,将某些内容放入 MY PROJECT/src/main/resources/static/
中将会生成 HTML 文件的内容。
所以我有以下问题:
您是否使用
<compile('org.springframework.boot:spring-boot-starter-web')
之类的方法将 SpringMVC 添加到 Spring Boot 项目中?您是否将静态内容放入“我的项目/src/main/resources/static/”中?
- 如果已经为您提供了 Controller ,为什么您还需要一个 Controller 来提供静态内容?尤其是映射到
/
的那个? - Angular 是否将 *.html 文件放入
MY PROJECT/src/main/resources/static/
中? - JavaScript 控制台说什么?找到
runtime.js
了吗?
请参阅“添加主页”部分中的https://spring.io/guides/gs/serving-web-content/
请参阅https://stackoverflow.com/questions/24661289/spring-boot-not-serving-static-content
内容,了解不该做什么(以及为什么)的其他想法。
本文最初发布于https://www.quora.com/How-can-spring-boot-serve-angular-static-files/answer/David-Talalayevsky
关于java - Spring Boot 不提供静态内容(Angular-CLI)来为 Tomcat 上的应用程序提供服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52227378/