Refreshing static content with Spring MVC and Boot

前端 未结 15 1967
迷失自我
迷失自我 2020-11-28 07:02

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,

相关标签:
15条回答
  • 2020-11-28 07:20

    The Java version of @viator 's answer:

    @Configuration
    class WebMvcConfigurer extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/dist/*.js").addResourceLocations(
                "file:src/main/typescript/dist/"
            );
        }
    }
    
    0 讨论(0)
  • 2020-11-28 07:20

    For Spring Boot 2+ with gradle Kotlin dsl:

    tasks.bootRun {
        sourceResources(sourceSets.getAt(SourceSet.MAIN_SOURCE_SET_NAME))
    }
    

    thanks to @briskr's answer for the gradle dsl version :)

    0 讨论(0)
  • 2020-11-28 07:22

    You can do it by just adding one more dependency

    you Gradle

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

    In you Pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <version>1.3.0.RELEASE</version>
    </dependency>
    
    0 讨论(0)
  • 2020-11-28 07:23

    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/

    0 讨论(0)
  • 2020-11-28 07:25

    You have two possebilities how to serve static webcontent

    1. From the classpath (per default src/main/resources/static or src/main/resources/public or META-INF/resources/)
    2. From the file system (per default src/main/webapp)

    If you pick solution 1) - you can safely copy the jar around as the static web content is within that jar. If you want that the server picks up changes, you need to do (auto)hotswapping.

    If you pick solution 2) - everything will work out of the box, every change will be automatically picked up. HOWEVER - if you copy the final jar to a different location - things will stop working. That is unless you specify an absolute path in application.properties. For example:

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

    So solution 2) is easier but less portable. Solution 1) is portable but more difficult to use(ide config).

    0 讨论(0)
  • 2020-11-28 07:30

    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.

    0 讨论(0)
提交回复
热议问题