Refreshing static content with Spring MVC and Boot

前端 未结 15 1969
迷失自我
迷失自我 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:30

    Ah ... I came across this issue too.

    Instead of putting your static content in the classpath src/main/resources/public folder, 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.

    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:

    <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:

    java -jar target/.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\webapp in src\main\resources\static.

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

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

    @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/'.

    Do not forget trailing slash.

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

    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

    I had the same problem and finally solved it by adding

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

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

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

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

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

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

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

    I am using 1.5.8.RELEASE.

    It instantly updates my changes especially static files or jsp files.

    If you are using Maven. You need to add this in pom.xml

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

    And you have to start Spring Boot with this:

    mvn clean spring-boot:run
    

    Full example and more detail here https://www.surasint.com/spring-boot-with-auto-update-changed-files-example/

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

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

    On the main menu, choose Run | Reload Changed Classes

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