配置Spring MVC应用程序来修改html文件/模板,而不需要重新编译



我有一个非常简单的Spring 4.0启动项目。我想启动应用程序,并能够对位于/templates/的html文件进行更改,而不必停止并重新启动应用程序。更改静态资产,如java脚本或css文件,是没有问题的。

下面是我的程序的详细信息:

没有XML配置文件。这个类用于配置。

@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/");
    }
}

这是我的控制器。

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

我有index.html位于templates/

我使用这个类运行应用程序。

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

您想要实现的目标很容易使用IDE完成,并且将在开发过程中节省大量时间。

首先,你需要配置Spring Boot不缓存thymleaf模板,通过设置:

spring.thymeleaf.cache=false

然后你只需要在调试模式下使用IDE启动应用程序(只需用main方法调试类),每当你对thyymeleaf模板进行更改时,你只需要指示IDE重新加载项目。

在IntelliJ IDEA中,这是从Run菜单中的Reload Changed Classes选项完成的。

我认为您可以配置Eclipse在每次更改时自动更新项目,但是我已经有一段时间没有使用它了。

项目路径

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

开发过程中模板的重新加载

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

开发过程中静态资源的重新加载

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

相关内容

最新更新