【问题标题】:Thymeleaf Not Hot Swapping IntellijThymeleaf 不热交换 Intellij
【发布时间】:2015-06-20 09:08:00
【问题描述】:

我在使用 Intellij 将 Thymeleaf 模板进行热交换/更新时遇到了一些问题。目前我必须重新启动整个服务器才能看到我的更改,这相当乏味并且减慢了我的工作流程。

我正在使用 Gradle、Intellij 14.1 和 Tomcat 8。我在调试模式下运行应用程序。

我已尝试将 Thymeleaf 设置为不可缓存。

@Configuration
public class ThymeleafConfig {

    @Autowired
    Environment environment;

    @Bean
    public ServletContextTemplateResolver templateResolver() {
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
        resolver.setPrefix(environment.getRequiredProperty("thymeleaf.resolver.prefix"));
        resolver.setSuffix(environment.getRequiredProperty("thymeleaf.resolver.suffix"));
        resolver.setTemplateMode(environment.getRequiredProperty("thymeleaf.resolver.templatemode"));
        resolver.setOrder(environment.getRequiredProperty("thymeleaf.resolver.order", Integer.class));
        resolver.setCacheable(environment.getRequiredProperty("thymeleaf.resolver.cacheable", Boolean.class));
        resolver.setCharacterEncoding(environment.getRequiredProperty("thymeleaf.resolver.character.encoding"));
        return resolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver());
        engine.addDialect(new LayoutDialect());
        engine.addDialect(new SpringSecurityDialect());
        return engine;
    }

    @Bean
    public ThymeleafViewResolver thymeleafViewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        return resolver;
    }
}

上面代码正在读取的属性文件。

# Thymeleaf
thymeleaf.resolver.prefix=/WEB-INF/views/
thymeleaf.resolver.suffix=.html
thymeleaf.resolver.templatemode=HTML5
thymeleaf.resolver.order=1
thymeleaf.resolver.cacheable=false
thymeleaf.resolver.character.encoding=UTF-8

我也尝试在 ApplicationInitializer 中设置它。

 @Override
    public void onStartup(ServletContext container) throws ServletException {

        /**
         * If no active profile is set via -Dspring.profiles.active then the application
         * will default to development mode
         */
        container.setInitParameter("spring.profiles.default", "dev");

        /**
         * Set thymeleaf cache to false if -Dspring.thymeleaf.cache is not passed
         */
        container.setInitParameter("spring.thymeleaf.cache", "false");

        /**
         * create the root Spring application context
         */
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.setDisplayName("app");
        rootContext.register(AppConfig.class);

        /**
         * manage the lifecycle of the root application context
         */
        container.addListener(new ContextLoaderListener(rootContext));

        /**
         * register and map the dispatcher servlet
         */
        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(rootContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

到目前为止,这些都没有奏效。

【问题讨论】:

    标签: java spring intellij-idea thymeleaf jvm-hotspot


    【解决方案1】:

    为部署选择爆炸战争。然后,您可以在点击 CMD + F10 时简单地更新资源或类和资源(我假设它可能是 Windows/Linux 上的 CTRL)。

    【讨论】:

    猜你喜欢
    • 2016-01-23
    • 2013-08-31
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多