【问题标题】:Spring boot not serving static content (angular-cli) to serve app on tomcatSpring Boot不提供静态内容(angular-cli)以在tomcat上提供应用程序
【发布时间】: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 启动并运行时它无法提供服务。

resources/static

我是否需要任何其他配置才能让 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 类型检查是 已启用。

替换 &lt;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,然后它显示内容

&lt;base href="/"&gt; 是问题还是脚本标签?

【问题讨论】:

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


    【解决方案1】:

    发生这种情况是因为 spring 尝试处理您的请求,但它不明白它们应该变成静态的。尝试添加以下配置:

    @Configuration
    public class MvcConfig implements WebMvcConfigurer {
    
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/ui/**").setViewName("/");
        }
    }
    

    addViewController("/ui/**") 是访问角度页面的模式

    【讨论】:

      【解决方案2】:

      在 Controller 方法上尝试将 @RequestMapping 注释只是一个建议

      像这样:

      @Controller
      @RequestMapping("/")
      public class AppController{ 
       }
      

      在 ComponentScan (basepackages{"com.packagename of modal classes"})

      【讨论】:

      • 我已经在使用@GetMapping了,这不是使用@RequestMapping(method = RequestMethod.GET)的组合方式吗?
      • 编辑了我的答案,看看是否有帮助
      【解决方案3】:

      没有像上面链接那样复杂的成功步骤:

      1. https://start.spring.io/
      2. 选择了一个 gradle 项目
      3. 添加了 Web 依赖项
      4. 已下载并取消归档结果(未从 demo 更改名称)
      5. 创建:demo/src/main/resources/static/index.html,内容如下:
        你在!
      6. demo 目录中发出以下命令以运行您的应用程序:./gradlew bootRun
      7. 打开浏览器并转到http://localhost:8080/

      您现在有证据表明将某些内容放入 MY PROJECT/src/main/resources/static/ 将生成您的 HTML 文件的内容。

      所以我有以下问题:

      • 您是否使用 compile('org.springframework.boot:spring-boot-starter-web') 之类的东西将 SpringMVC 添加到您的 Spring Boot 项目中?

      • 您是否将静态内容放入 MY PROJECT/src/main/resources/static/ 中?

      • 如果为您提供了控制器,为什么还需要控制器来提供静态内容?尤其是映射到/?
      • 是否有角度将 *.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

      【讨论】:

      • 感谢@DavitT 的回复。请在我的帖子末尾查看您的问题的答案。我刚刚更新了它们。
      • 我关注了演示 gradle 项目,并确定 MY PROJECT/src/main/resources/static/ 中的内容在 8080 上显示 index.html 的内容。但我不明白它是怎么回事与我目前在同一路径中的不同,除了那些是角度生成的 .html 和 .js 文件,为什么不接。
      • 虽然在我的情况下,我在网页上看到了 200 的检查和 [ ] 响应。
      • @Soc 所以现在你已经将问题缩小到角度输出和你的网页有问题的事实。您确实需要查看控制台并学习阅读它(请参阅webnots.com/how-to-use-developer-tools-in-chrome 之类的内容,特别是第 4 节,但所有其他内容也很重要)。首先告诉 Angular 输出一些超级简单的东西,比如“Hello World”。如果不是,那就是您的网页,如果是,那么就是您的一些 JavaScript 代码。然后遵循这两条路径。
      【解决方案4】:

      这是因为您使用了隐式包含@ResponseBody 的@RestController 注释。您必须使用 @Controller 进行 Spring boot 以自动加载 html 模板。

      @Controller
      public class AppController {
          @GetMapping("/")
          public String index() {
              return "index";
          }
       }
      

      【讨论】:

      • 我也尝试过使用 @Controller 注释,当浏览到 localhost:8080 时,网页上仍然没有显示任何 UI 内容,只是 [ ]。虽然我注意到 chrome 选项卡上的 Angular 符号,所以它正在尝试获取内容,但在 UI 显示上什么也没有。
      • 哦,好的,你必须更改默认文件夹,这可以帮助你:logicbig.com/tutorials/spring-framework/spring-boot/…
      • 所以从技术上讲,无论我在类路径(/resources、/static、/public 等)启动 tomcat 的文件夹之一中放置什么内容,它都应该获取 index.html 和相关内容。即使我明确指定并设置了 spring.resources.static-locations(参见上面的更新代码),localhost:8080 仍然没有显示 UI。
      • 实际上,我插入了一个 page1.html,其中只有一个标题,以验证 tomcat 是否从资源文件夹中获取任何内容,但似乎没有。我在网页上也看不到它的内容。
      • 你可以尝试用“@SpringBootApplication”替换“@Configuration @EnableAutoConfiguration @ComponentScan({"webapp"})”吗?
      猜你喜欢
      • 2015-05-24
      • 2019-03-30
      • 2019-09-20
      • 2018-09-17
      • 1970-01-01
      • 2011-04-18
      • 2016-07-14
      • 2019-10-20
      • 2021-05-05
      相关资源
      最近更新 更多