2016-10-15 7 views
2

私はここと文書で見つけたすべてのヒントを試しましたが、まだ運がありません。私はThymeleafでSpring webappを持っています。 IDEAで更新を呼び出すと、リソースとテンプレートは再ロードされません(リロードすると何も表示されません)。私はctrl + f5を押して、ブラウザで狂ったようにすることができます。変更はそこにはありません。春のブートとThymeleaf - テンプレートとリソースのホットスワップをもう一度

すべては、このように1つのJavaクラスで構成されています

@EnableWebMvc 
public class MvcConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware { 

マイフォルダ構造は今 thisのように見えますが、私はまた、「静的」フォルダなしまたはWebアプリケーション/リソースへのリソースを入れてみました。

ResourceHandlerRegistry:

@Override 
public void addResourceHandlers(final ResourceHandlerRegistry registry) { 
    super.addResourceHandlers(registry); 
    registry.addResourceHandler("/img/**").addResourceLocations("classpath:/static/img/"); 
    registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/"); 
    registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/"); 
} 

私は両方のapplication.propertiesに誤ったキャッシュ=を指定:

spring.thymeleaf.cache=false 

と述べたMvcConfigクラスで:

@Bean 
public SpringResourceTemplateResolver templateResolver() { 
    SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); 
    templateResolver.setApplicationContext(this.applicationContext); 
    templateResolver.setPrefix("/WEB-INF/templates/"); 
    templateResolver.setSuffix(".html"); 
    templateResolver.setTemplateMode(TemplateMode.HTML); 
    templateResolver.setCacheable(false); 
    return templateResolver; 
} 

をSOにいくつかの回答によると、私はdevtoolsの依存関係を追加しました:

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

まだ動作しません。いくつかは、真addResources =とMavenのブートプラグインを追加するために言ったので、私はやった:私は、更新を呼び出すときに、私のJavaクラスはすぐに再ロードされるため、

<plugin> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-maven-plugin</artifactId> 
    <version>1.4.1.RELEASE</version> 
    <configuration> 
     <addResources>true</addResources> 
    </configuration> 
</plugin> 

私の考えは、私は推測し適切に設定されています。リソースとhtmlファイルだけが、私はそれのためにサーバーを再起動する必要がありますされていません。実際の* .htmlファイルはそれほど大きな問題ではありませんが、小さなcssやjsの変更があったら毎回サーバーを再起動することが多く、私は何が間違っているかを知りながら15時間ほど失ってしまい、本当にイライラしました。

ご協力いただきますようお願い申し上げます。

+0

私が知ったことは、実際には "ターゲット"フォルダに反映されますが、実行中のアプリケーション自体には反映されません。 – Luke

+0

これは私のためのトリックでしたhttp://stackoverflow.com/questions/21399586/hot-swapping-in-spring-boot/43114954#43114954 – shabby

答えて

0

さて、私は私の特定の事例に答えました。問題は私のアプリではまったくではなかったし、設定されている(おそらく)。代わりにTomcat 8.5.5を使用しました。Tomcat 7に切り替えました。すべてが正しく機能するようになりました。なぜ誰かが知っていますか?

2

私はそれにしばらく時間を費やしており、最後に私はそれがどのように機能しているかを説明します。あなたの周りの グーグルは、いくつかの情報を見つけることは:

春ブーツapplication.propertiesSO - Netbeans 8 won't reload static Thymeleaf files

マイinitalアプローチ

  • Spring Boot hot swap
  • SO - Spring Boot + Jetty & hot deployment
  • は、キャッシュを無効にし、春のdevのツールを追加しました

    spring.thymeleaf.cache=false 
    spring.thymeleaf.mode=LEGACYHTML5 
    spring.thymeleaf.prefix=/templates/ 
    

    pom。プロジェクト(のIntelliJアイデアでCTRL + F9)を行う場合にのみ、ホットスワップが行われているのでしかし、上記のスニペットを使用してXMLを

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

    十分ではありません。これは、デフォルトのテンプレートリゾルバがクラスパスに基づいていることが原因であり、これが再コンパイルが必要な理由です。

    application.properties

    spring.thymeleaf.cache=false 
    spring.thymeleaf.mode=LEGACYHTML5 
    spring.thymeleaf.templates_root=src/main/resources/templates/ 
    

    ApplicationクラスIが見つけ

    @SpringBootApplication 
    public class MyApplication { 
    
        @Autowired 
        private ThymeleafProperties properties; 
    
        @Value("${spring.thymeleaf.templates_root:}") 
        private String templatesRoot; 
    
        public static void main(String[] args) { 
         SpringApplication.run(MyApplication.class, args); 
        } 
    
        @Bean 
        public ITemplateResolver defaultTemplateResolver() { 
         FileTemplateResolver resolver = new FileTemplateResolver(); 
         resolver.setSuffix(properties.getSuffix()); 
         resolver.setPrefix(templatesRoot); 
         resolver.setTemplateMode(properties.getMode()); 
         resolver.setCacheable(properties.isCache()); 
         return resolver; 
        } 
    } 
    


    作業溶液は、ファイルシステムベースのリゾルバを使用してdefaultTemplateResolverをオーバーライドすることですこのソリューションoptiあなたは構成を外部化し、異なるプロファイル(dev、prodなど)を使用することができます。F5を押すだけで変更をリロードすることができます。

関連する問題