java Spring Boot 替代索引页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28602131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:52:53  来源:igfitidea点击:

Spring Boot alternative index page

javaspringspring-mvcconfigurationspring-boot

提问by KenavR

My app uses Spring Boot on the back end and a SPA (Angular) site on the front end. Currently I am serving the index.html page from the webappfolder, which works automatically no configuration needed. Now I integrated a build process for the front end using gulp and all the created sources are "copied" into a builddirectory. Now I would like to serve the index.htmlfile from the builddirectory as my main page.

我的应用程序在后端使用 Spring Boot,在前端使用 SPA(Angular)站点。目前我正在从webapp文件夹中提供 index.html 页面,该页面无需配置即可自动工作。现在我使用 gulp 为前端集成了一个构建过程,并将所有创建的源“复制”到一个build目录中。现在我想将目录中的index.html文件build作为我的主页。

I tried spring.application.index=build/index.htmland some other spring boot settings, but nothing worked. I believe no code is needed from my current code base but if anything is missing let me know.

我尝试spring.application.index=build/index.html了其他一些弹簧启动设置,但没有任何效果。我相信我当前的代码库不需要任何代码,但如果缺少任何内容,请告诉我。

Is there a way to configure this in the applications.propertiesfile? Do I need to create a controller for the index page? Or is there any other way to change the default behavior?

有没有办法在applications.properties文件中配置它?我需要为索引页创建一个控制器吗?或者有没有其他方法可以更改默认行为?

thanks

谢谢

回答by Steve

Going by the common Spring Boot properties, you should be able to modify this property:

根据常见的 Spring Boot 属性,您应该能够修改此属性:

spring.application.index=

Admittedly, I do tend to create a minimal 'home' controller with a @RequestMapping("/")anyway. :)

不可否认,我确实倾向于创建一个最小的“家庭”控制器@RequestMapping("/")。:)

It's worth noting that the builddirectory will only be on the classpath if it's under src/main/resources. It's also worth mentioning that the contents of src/main/webappdon't get bundled into the jar automatically. src/main/resources/staticis where Spring Boot will look for your static web files. As such, there are a couple of alternatives for you.

值得注意的是,如果build目录在src/main/resources. 还值得一提的是, 的内容src/main/webapp不会自动捆绑到 jar 中。src/main/resources/static是 Spring Boot 将查找您的静态 Web 文件的地方。因此,您有几种选择。

Option 1: Configure your Grunt build to output to a directory under src/main/resources/static.

选项 1:将您的 Grunt 构建配置为输出到src/main/resources/static.

Option 2: Configure your Java build tool to take the Grunt output and put it in your resources directory, so that it's on the classpath. For example, using Maven, the following would move the contents of a directory called buildinto your src/main/rescources/static.

选项 2:配置您的 Java 构建工具以获取 Grunt 输出并将其放在您的资源目录中,以便它位于类路径中。例如,使用 Maven,以下内容会将名为的目录的内容移动build到您的src/main/rescources/static.

<plugin>
  <artifactId>maven-resources-plugin</artifactId>
  <version>2.6</version>
  <executions>
    <execution>
      <id>copy-resources</id>
      <phase>validate</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <outputDirectory>${basedir}/target/classes/static</outputDirectory>
        <resources>
          <resource>
            <directory>build</directory>
            <filtering>true</filtering>
          </resource>
        </resources>
      </configuration>
    </execution>
  </executions>
</plugin>