首页 文章

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

提问于
浏览
38

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

我从"Building a RESTful Web Service with Spring Boot Actuator"指南(http://spring.io/guides/gs/actuator-service/)设置了我的项目 . 基本上它使用Spring Boot和MVC控制器来创建REST服务 . 另外,我使用了"Consuming a RESTful Web Service with AngularJS" guide(http://spring.io/guides/gs/consuming-rest-angularjs/)来构建AngularJS的前端 . 它创建一个显示REST服务响应的网页 . 我做的唯一更改是请求发送到我的应用程序而不是“http://rest-service.guides.spring.io/greeting ". My static content is stored in "src / main / resources / public”文件夹 . 此设置正常工作,但不重新加载静态内容 .

14 回答

  • 8

    你有两个拥有者如何提供静态webcontent

    • 来自类路径(默认为src / main / resources / static或src / main / resources / public或META-INF / resources /)

    • 来自文件系统(默认为src / main / webapp)

    如果您选择解决方案1) - 您可以安全地复制jar,因为静态Web内容在该jar中 . 如果您希望服务器获取更改,则需要执行(自动)hotswapping .

    如果您选择解决方案2) - 一切都将开箱即用,每个更改将自动获取 . 但是 - 如果你将最后一个 jar 复制到另一个位置 - 事情就会停止工作 . 这是除非您在 application.properties 中指定绝对路径 . 例如:

    spring.resources.static-locations=file:///C:/myspringbootapp/src/main/webapp
    

    所以解决方案2)更容易,但便携性更差 . 解决方案1)是可移植的但更难以使用(ide配置) .

  • 0

    文档说"all modern IDEs allow reloading of static resources and usually also hot-swapping of Java class changes"(http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-hotswapping) . 它's true. Eclipse does it more or less by default, and I' m不是IntelliJ用户,但根据我的理解,您可以将其配置为自动构建 .

  • 1

    回顾原始问题

    我遇到了一个问题,当我修改我的静态内容(html,js,css)时,我每次都要重启应用程序

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

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

    pom.xml spring-boot-maven-plugin 我对这个spring-boot-devtools事情感到困惑,但无论我做什么都没有效果 .

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

    你的道路很好 . src / main / resources / static也没关系 .

  • 11

    啊......我也遇到过这个问题 .

    不要将静态内容放在classpath src/main/resources/public 文件夹中,而是将它们放在 src/main/webapp 中,与其他任何Java Web应用程序相同 . 嵌入式Tomcat会在更改时自动重新加载它们 .

    如注释中所述,默认配置将不包括 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>
    

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

    java -jar target / .jar

    在运行时,您可以使用Chrome开发工具或您喜欢的任何IDE来修改文件,而无需重新启动 . 但是,无论何时运行构建,生成的包都将包含 src\main\resources\staticsrc\main\webapp 下的所有文件 .

  • 1

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

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

  • 1

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

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

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

    Do not forget trailing slash.

  • 1

    我有同样的问题,这里提出的解决方案似乎是合乎逻辑的,并且对我来说很简单:1- ctrl shift A 2-搜索注册表3-在打开的对话框中搜索"compiler.automake.allow.when.app.running"并检查它http://garywaddell.com/2015/11/20/spring-boot-intellij-idea-not-reloading-static-content/

  • 2

    对于eclipse,您必须激活项目 - >“自动构建”选项作为最低配置 .

  • 1

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

    如果您使用我用于设置项目的JHipster,则会为您配置Grunt,browsersync,spring boot和angularjs .

    当然,这需要比IDE更多的工具,而且要复杂得多,所以我不建议每个项目使用它 .

  • 25

    spring-boot-devtools是 not 编辑静态htm / js的"hot deploy"的解决方案

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

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

  • 4

    我正在使用1.5.8.RELEASE .

    它会立即更新我的更改,尤其是静态文件或jsp文件 .

    如果你使用Maven . 您需要在pom.xml中添加它

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    

    你必须用这个启动Spring Boot:

    mvn clean spring-boot:run
    

    完整示例和更多详细信息https://www.surasint.com/spring-boot-with-auto-update-changed-files-example/

  • 24

    @viator的Java版本答案:

    @Configuration
    class WebMvcConfigurer extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/dist/*.js").addResourceLocations(
                "file:src/main/typescript/dist/"
            );
        }
    }
    
  • 1

    @eigil为maven build提到了 addResources config . 我在gradle构建中使用了 spring-boot-gradle-plugin ,我发现了这个Spring Boot github issue和Spring Boot doc mentioned this option too . 只需将此指令添加到build.gradle并运行Gradle任务 bootRun ,然后资源文件在保存时立即刷新 . 仅供参考 .

  • 1

    你可以通过添加一个依赖项来实现

    你Gradle

    compile group: 'org.springframework.boot', name: 'spring-boot-devtools', version: '1.3.0.RELEASE'
    

    在你的Pom.xml中

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <version>1.3.0.RELEASE</version>
    </dependency>
    

相关问题