Java 使用 Spring MVC 和 Boot 刷新静态内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24762657/
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-08-14 14:43:57  来源:igfitidea点击:

Refreshing static content with Spring MVC and Boot

javaspring-mvcspring-boot

提问by palto

I'm evaluating Spring MVC & Boot and AngularJs for building web applications. I've run into the problem that when I make modifications to my static content (html, js, css), I have to restart the application every time. I hope there is a some way of solving that because restarting the whole application for static content changes is not efficient. Every other web app framework I've tried allows updating static content files on the fly(even just Spring MVC and plain old WAR application).

我正在评估用于构建 Web 应用程序的 Spring MVC & Boot 和 AngularJs。我遇到了一个问题,当我对我的静态内容(html、js、css)进行修改时,我每次都必须重新启动应用程序。我希望有某种方法可以解决这个问题,因为为静态内容更改重新启动整个应用程序效率不高。我尝试过的所有其他 Web 应用程序框架都允许动态更新静态内容文件(甚至只是 Spring MVC 和普通的旧 WAR 应用程序)。

I've setup my project from "Building a RESTful Web Service with Spring Boot Actuator" guide (http://spring.io/guides/gs/actuator-service/). Basically it uses Spring Boot and MVC controllers to create a REST service. In addition, I've used "Consuming a RESTful Web Service with AngularJS" guide (http://spring.io/guides/gs/consuming-rest-angularjs/) to build a frontend with AngularJS. It creates a web page that displays the response from the REST service. The only change I've made is that the requests are made to my application instead of "http://rest-service.guides.spring.io/greeting". My static content is stored in "src/main/resources/public" folder. This setup works correctly except it doesn't reload static content.

我已经从“使用 Spring Boot Actuator 构建 RESTful Web 服务”指南(http://spring.io/guides/gs/actuator-service/)设置了我的项目。基本上它使用 Spring Boot 和 MVC 控制器来创建 REST 服务。此外,我已经使用“使用 AngularJS 使用 RESTful Web 服务”指南 ( http://spring.io/guides/gs/sumption-rest-angularjs/) 使用 AngularJS 构建前端。它创建一个网页,显示来自 REST 服务的响应。我所做的唯一更改是向我的应用程序发出请求,而不是“ http://rest-service.guides.spring.io/greeting”。我的静态内容存储在“src/main/resources/public”文件夹中。除了不重新加载静态内容外,此设置可以正常工作。

采纳答案by Dave Syer

The docs say "all modern IDEs allow reloading of static resources and usually also hot-swapping of Java class changes" (https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/html/howto.html#howto-hotswapping). It's true. Eclipse does it more or less by default, and I'm not an IntelliJ user, but from what I understand you can configure it to build automatically as well.

文档说“所有现代 IDE 都允许重新加载静态资源,通常还允许热交换 Java 类更改”(https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/html/howto。 html#howto-hotswapping)。这是真的。Eclipse 默认情况下或多或少会这样做,我不是 IntelliJ 用户,但据我所知,您也可以将其配置为自动构建。

回答by Steve

Ah ... I came across this issue too.

啊……这个问题我也遇到了。

Instead of putting your static content in the classpath src/main/resources/publicfolder, put them in src/main/webapp, the same as you would any other Java web app. The embedded Tomcat will automatically reload them whenever they change.

不要将您的静态内容放在 classpathsrc/main/resources/public文件夹中,而是将它们放在 中src/main/webapp,就像您将任何其他 Java Web 应用程序一样。嵌入式 Tomcat 会在它们发生变化时自动重新加载它们。

As mentioned in the comments, the default configuration will not include the resources that are in src\main\webapp. To get around this issue, you can just add the following to your pom.xml <build>node:

如评论中所述,默认配置不会包含src\main\webapp. 要解决此问题,您只需将以下内容添加到 pom.xml<build>节点:

<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>src/main/webapp</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

By using the resources plugin, you are able to do your local development by running the executable JAR:

通过使用资源插件,您可以通过运行可执行 JAR 来进行本地开发:

java -jar target/.jar

java -jar 目标/.jar

While that is running you can use Chrome Dev Tools or whatever IDE you like for modifying the files, without restarts. However, whenever you run your build, then the package generated will include all of the files under src\main\webappin src\main\resources\static.

在运行时,您可以使用 Chrome Dev Tools 或任何您喜欢的 IDE 来修改文件,而无需重新启动。但是,只要你运行你的身材,那么所产生的方案将包括在所有的文件src\main\webappsrc\main\resources\static

回答by hyness

A colleague and I came across this issue as well. We found the answer in the IntelliJ documentation...

我和一位同事也遇到了这个问题。我们在IntelliJ 文档中找到了答案......

On the main menu, choose Run | Reload Changed Classes

在主菜单上,选择运行 | 重新加载更改的类

回答by tObi

For eclipse you have to activate the Project -> "Build Automatically" option as a minimum configuration.

对于 eclipse,您必须激活 Project -> "Build Automatically" 选项作为最低配置。

回答by palto

What I ended up using was Browsersync with grunt. browsersync and grunt watches your static resources on disk and updates the browser when you edit the files. It acts as a kind of proxy. This way you can see changes in UI immediately without building or restarting anything.

我最终使用的是Browsersync 和 grunt。browsersync 和 grunt 监视磁盘上的静态资源并在您编辑文件时更新浏览器。它充当一种代理。通过这种方式,您可以立即查看 UI 中的更改,而无需构建或重新启动任何内容。

Grunt, browsersync, spring boot and angularjs are configured for you if you use JHipsterwhich I used to setup my project.

咕噜,browsersync,弹簧引导和angularjs配置为你,如果你使用JHipster这在我以前设置我的项目。

Granted this requires a lot more tools than just an IDE and is a lot more complicated so I wouldn't recommend this for every project.

当然,这需要更多的工具,而不仅仅是一个 IDE,而且要复杂得多,所以我不会为每个项目推荐这个。

回答by ameen

I had the same issue , the solution proposed here seems logical and worked for me in breif : 1- ctrl+shift+A 2- search for registry 3- in the opened dialogue search for "compiler.automake.allow.when.app.running" and check it http://garywaddell.com/2015/11/20/spring-boot-intellij-idea-not-reloading-static-content/

我遇到了同样的问题,这里提出的解决方案似乎合乎逻辑,并且在 breif 中对我有用:1- ctrl+shift+A 2- 搜索注册表 3- 在打开的对话框中搜索“compiler.automake.allow.when.app”。正在运行”并检查它 http://garywaddell.com/2015/11/20/spring-boot-intellij-idea-not-reloading-static-content/

回答by eigil

A recap of the original problem

重述原始问题

I've run into the problem that when I make modifications to my static content (html, js, css), I have to restart the application every time

我遇到了一个问题,当我对我的静态内容(html、js、css)进行修改时,我每次都必须重新启动应用程序

I had the same problem and finally solved it by adding

我遇到了同样的问题,最后通过添加解决了它

<configuration>
    <addResources>true</addResources>
</configuration>

to spring-boot-maven-pluginin the pom.xmlI got confused by this spring-boot-devtools thing, but it had no effect whatever I did.

spring-boot-maven-pluginpom.xml我被这个弹簧引导devtools事情混淆,但它没有任何效果什么我做到了。

My static content is stored in "src/main/resources/public" folder.

我的静态内容存储在“src/main/resources/public”文件夹中。

Your path is just fine. src/main/resources/static is also fine.

你的路径很好。src/main/resources/static 也很好。

回答by viator

My solution (written in Kotlin but is quite obvious):

我的解决方案(用 Kotlin 编写但很明显):

@Controller
class WebController : WebMvcConfigurerAdapter() {

    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
        System.getProperty("resources.local.path")?.let {
            registry.addResourceHandler("/**").addResourceLocations(it)
        }
    }
...
}

Main idea is you can add your own resource handler conditionally. E.g. if some system property is set (resources.local.path) then add resource location with value from the property. Then you set this property in development with some reasonable value like '-Dresources.local.path=file:/Users/andrey/Projects/gsp-test/src/main/resources/static/'.

主要思想是您可以有条件地添加自己的资源处理程序。例如,如果设置了某些系统属性(resources.local.path),则使用属性值添加资源位置。然后您在开发中使用一些合理的值设置此属性,例如“-Dresources.local.path=file:/Users/andrey/Projects/gsp-test/src/main/resources/static/”。

Do not forget trailing slash.

不要忘记尾部斜杠。

回答by pbirnie

spring-boot-devtools is notthe solution to "hot deploy" of edited static htm/js

spring-boot-devtools不是编辑静态 htm/js 的“热部署”的解决方案

I configured a web facet inside intellij so that when I use it to edit html/js file inside resources/static, intellij then knows to copy the updated file to ./target and the spring boot application I have launched inside the automatically displays that content

我在 intellij 中配置了一个 web facet,这样当我使用它来编辑 resources/static 中的 html/js 文件时,intellij 就会知道将更新的文件复制到 ./target 并且我在其中启动的 spring boot 应用程序会自动显示该内容

see https://www.jetbrains.com/help/idea/2016.2/configuring-static-content-resources.html

https://www.jetbrains.com/help/idea/2016.2/configuring-static-content-resources.html

回答by briskr

@eigil metioned addResourcesconfig for maven build. I'm using spring-boot-gradle-pluginin a gradle build, and I found this Spring Boot github issue, and the Spring Boot doc mentioned this option too. Just add this directive to build.gradle and run Gradle task bootRun, then resource file refreshes immediately when saved. FYI.

@eigil 提到addResources了 Maven 构建的配置。我spring-boot-gradle-plugin在 gradle 构建中使用,我发现了这个Spring Boot github 问题,并且 Spring Boot 文档也提到了这个选项。只需将此指令添加到 build.gradle 并运行 Gradle task bootRun,然后资源文件在保存时立即刷新。供参考。