Michael
Michael

Reputation: 455

Configure Spring MVC application to make changes to html files/templates without recompiling

I have a very simple Spring 4.0 Boot project. I would like to start the application and be able to make changes to the html files located in /templates/ on the fly, without having to stop and restart the application. Changes to static assets, like java scripts or css files, is no problem.

Below are the details of my program:

There are no XML configuration files. This class is used for configuration.

@Configuration
public class MVCConfiguration extends WebMvcConfigurerAdapter{

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

          registry.addResourceHandler("assets/**")
            .addResourceLocations("classpath:/templates/assets/");
          registry.addResourceHandler("/css/**")
            .addResourceLocations("/css/");
          registry.addResourceHandler("/img/**")
            .addResourceLocations("/img/");
          registry.addResourceHandler("/js/**")
            .addResourceLocations("/js/");

    }
}

This is my controller.

@Controller
public class ControlFreak {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(){
        return "index";
    }
}

I have index.html located in templates/

I run the application using this class.

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }
}

Upvotes: 0

Views: 2774

Answers (2)

Юлий Щербак
Юлий Щербак

Reputation: 95

Path to project

project.base-dir=file:///C:/temp/auth/

Templates reloading during development

spring.thymeleaf.prefix=${project.base-dir}/src/main/resources/templates/
spring.thymeleaf.cache=false

Static resources reloading during development

spring.resources.static-locations=${project.base-dir}/src/main/resources/static/
spring.resources.cache-period=0

Upvotes: 0

geoand
geoand

Reputation: 64011

What you are trying to achieve is easily done using an IDE and will save a heck of a lot of time during development.

First of all you need to configure Spring Boot to not cache Thymeleaf templates by setting:

spring.thymeleaf.cache=false

Then you just need to start the application using the IDE in debug mode (just Debug the class with the main method) and whenever you make change to a Thymeleaf Template you just need to instruct the IDE to reload the project.

In IntelliJ IDEA, that is done from the Reload Changed Classes option in the Run menu.

I think you can configure Eclipse to automatically update the project on each change, but it's been a while since I have used it.

Upvotes: 1

Related Questions