【问题标题】:Netbeans 8 won't reload static Thymeleaf filesNetbeans 8 不会重新加载静态 Thymeleaf 文件
【发布时间】:2014-11-24 01:54:54
【问题描述】:

我正在通过 Maven 使用 Spring Boot 和 Thymeleaf。当我进行更改时,我似乎无法让 Netbeans 自动重新部署我的任何 Thymeleaf 模板文件。为了查看更改,我需要进行完整的清理/构建/运行。这需要的时间太长了。

模板位于src/main/resources/templates。我在 src/main/resources/ 中有一个带有 spring.thymeleaf.cache=falsespring.template.cache=false 的 application.properties 文件。

我在项目设置中打开了“Compile on save”、“Copy resources on save”和“Deploy on save” .

我的 maven 构建生成了一个战争文件,Netbeans 将其部署到 Tomcat,我正在使用注释 @EnableAutoConfiguration

Netbeans 热部署对 Java 类的更改,但不会热部署 src/main/resources/ 中的任何静态文件。

正在使用的软件:

  • Mac OS X 10.9.4
  • Java 1.8
  • Netbeans 8.0.1
  • Tomcat 8.0.12
  • Spring Boot 1.1.7
  • Thymeleaf 2.1.3(通过 Spring Boot)

非常感谢任何指导。

【问题讨论】:

  • 资源根目录下的文件是否开启了过滤?这些不是由 netbeans afaik 复制的。
  • 我有段时间没用过Netbeans了,顺便问一下,你是在简单的运行还是调试项目?
  • @mkleint - 开启过滤是什么意思?静态文件在 src/main/resources 中。
  • @geoand - 如果我调试或正常运行,它没有任何区别。无论哪种情况,Netbeans 都会对 Java 文件进行热重载,但不会对 src/main/resources 中的静态文件进行热重载。
  • pom 中的 元素可以有一个子元素 基本上意味着资源不是简单地复制而是处理(或多或少简单的模板)。 netbeans 不会复制此类资源文件。

标签: spring maven netbeans spring-boot thymeleaf


【解决方案1】:

一个选项是考虑配置 Thymeleaf 的 FileTemplateResolver

要使用 Spring Boot 执行此操作,请定义一个实现 ITemplateResolver 接口的 bean,其名称为 defaultTemplateResolver,如果存在,Spring Boot 将采用它而不是默认值,这是如何完成的,假设你激活组件扫描,以便自动获取此配置类:

@Configuration
public class ThymeleafConfiguration {
  @Bean
  public ITemplateResolver defaultTemplateResolver() {
    TemplateResolver resolver = new FileTemplateResolver();
    resolver.setSuffix(".html");
    resolver.setPrefix("path/to/your/templates");
    resolver.setTemplateMode("HTML5");
    resolver.setCharacterEncoding("UTF-8");
    resolver.setCacheable(false);
    return resolver;
  }
}

prefix 应该是一个相对路径,当添加到您的运行时工作目录 (cwd) 时,将解析为模板目录。如果您不确定,请将其设置为完整的绝对路径,但是上面的 bean 就没有意义了。由于将spring.thymeleaf.prefix 属性设置为绝对路径可能会产生相同的效果。

【讨论】:

    【解决方案2】:

    一直在为我的 eclipse+thymeleaf+sprint 启动动态重新加载模板寻找解决方案,同时记录...。

    最后我在这里找到了这个问题,spring.thymeleaf.cache=false 和 spring.template.cache=false 解决了我的问题。

    【讨论】:

    • 我只花了 30 分钟来解决同样的问题......看起来有一些旧博客指向属性 spring.template.cache。甚至在 spring boot github 网站上也有一个关于同一件事的 issue :S
    【解决方案3】:

    除了将 Thymeleaf 视图设置为不可被 ie 缓存。 spring.thymeleaf.cache=false 在您的 application.properties 中, 尝试在 pom.xml 中明确定义资源目录:

    <build>      
        ...
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        ...
    </build>
    

    【讨论】:

    • 这对我来说完美无缺。只需要添加“资源”声明。无需更改 application.properties。
    【解决方案4】:

    为了解决这个问题,pom.xml 中的 spring-boot-maven-plugin 应该是这样的:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>springloaded</artifactId>
                <version>1.2.0.RELEASE</version>
            </dependency>
        </dependencies>
    </plugin>
    

    并将其添加到您的应用程序属性中:

    spring.thymeleaf.cache=false
    

    它通常也适用于 Spring bean。

    【讨论】:

      【解决方案5】:

      只是说这很适合我使用 Tomcat 的外部实例:

      • 使用 JRebel 或 Spring Loaded javaagent 作为 VM 选项运行 Tomcat
      • 关闭“保存时编译”、“保存时复制资源”和“保存时部署”
      • 在 Netbeans 中添加执行编译目标的自定义操作
      • 当你想查看更新时运行它

      http://wiki.netbeans.org/MavenBestPractices#Binding_Maven_goals_to_IDE_actions

      https://github.com/spring-projects/spring-loaded

      https://zeroturnaround.com/software/jrebel/quickstart/standalone/

      或者您可以使用带有 spring-boot-maven-plugin 和 Spring Loaded 的嵌入式 tomcat,那么您将不需要编译操作:

      https://docs.spring.io/spring-boot/docs/current/reference/html/howto-hotswapping.html

      【讨论】:

        【解决方案6】:

        我也有这个问题。我注意到 Netbeans 会自动重新加载

        中的网页

        /src/main/webapp/

        您必须将所有模板从 /src/main/resources/templates 移动到此目录。

        您还必须更改 application.properties 文件中的 spring boot 属性:

        spring.thymeleaf.prefix=templates/

        这对我有用

        【讨论】:

          【解决方案7】:

          我在 Netbeans 8.0.2 和 Windows 上遇到了同样的问题。我正在构建一个要部署到 Tomcat 的 WAR,但我想尝试一下 Spring Boot。看起来较新版本的 Netbeans 可能会使用 Spring Boot 插件或使用 Eclipse 解决此问题。用这样的小东西交换 IDE 似乎很疯狂。我尝试了所有我能找到的建议;弹簧加载,缓存属性,扩展 TemplateResolver ......我无法让它们中的任何一个工作。我终于偶然发现了this 博客并按照这些说明解决了我的问题。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-04-30
            • 1970-01-01
            • 1970-01-01
            • 2017-10-08
            • 2016-08-17
            • 2017-04-05
            • 1970-01-01
            相关资源
            最近更新 更多