【发布时间】:2019-02-13 02:13:56
【问题描述】:
我正在努力解决 spring-boot 没有在 8080 上提供前端文件(通过 angular 创建)的问题。我运行 ng build 将 .ts ng 组件文件转换为 .js 并在 outputPath 中生成:/资源/静态文件夹(我在 angular.json 文件中设置了 outputPath)。
创建了一个控制器来提供来自资源/静态的内容。
@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);
}
}
Ran 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 提供 Angular 前端代码?任何建议表示赞赏。
@DavidT 回答您的问题:
是的 build.gradle:
compile('org.springframework.boot:spring-boot-starter-web:2.0.2.RELEASE')此处为静态内容: MYPROJECT/MYPROJECT-web/src/main/resources/static/
由于 spring-boot 没有从路径中选择角度静态内容,我 建议添加控制器的参考在线资源 专门为它服务。 [我有 3 ng 组件可访问- 登录:http://localhost:4200/,首页:http://localhost:4200/home,销售:http://localhost:4200/sale。但 访问tomcat端口我想把文件放在静态下;休息 Angular 将负责路由。]
当从 frotnend 目录运行
ng build时,会在 frontend/src 目录下生成 angular 文件。我可以手动复制、手动粘贴以放入静态文件夹或更改outputPath以指向 angular.json 中的资源/静态,直接将其放在运行 ng build cmd 上。精确的 以上资源/静态截图中的文件。我没有使用 javascript 控制台,而是使用浏览器打开 index.html IntellijIdea 的选项,在网页上检查我看到错误 -
拒绝从 '' 执行脚本,因为它的 MIME 类型 ('text/html') 不可执行,严格的 MIME 类型检查是 已启用。
替换 <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="/"> 是问题还是脚本标签?
【问题讨论】:
标签: java spring spring-mvc spring-boot tomcat