Spring Boot adding thymeleaf-layout-dialect -


i using spring boot (v1.5.3.build-snapshot) new spring boot.

using gradle

note normal thymeleaf dialect works fine (th:...) spring-boot-starter-thymeleaf\1.5.3.build-snapshot

i want add thymeleaf-layout-dialect added dependency

compile('nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect') 

the documentation says add dialect doing following

templateengine templateengine = new templateengine();  // or  springtemplateengine spring config templateengine.adddialect(new layoutdialect()); 

so added configuration class

@configuration public class myconfiguration {   @bean public springtemplateengine templateengine(){     springtemplateengine templateengine  = new springtemplateengine();     templateengine.adddialect(new layoutdialect());     return templateengine; } } 

but when try running app following error

org.thymeleaf.exceptions.configurationexception: cannot initialize: no template resolvers have been set @ org.thymeleaf.configuration.initialize(configuration.java:203) ~[thymeleaf-2.1.5.release.jar:2.1.5.release] @ org.thymeleaf.templateengine.initialize(templateengine.java:827) ~[thymeleaf-2.1.5.release.jar:2.1.5.release] @ org.thymeleaf.spring4.view.thymeleafview.renderfragment(thymeleafview.java:203) ~[thymeleaf-spring4-2.1.5.release.jar:2.1.5.release] @ org.thymeleaf.spring4.view.thymeleafview.render(thymeleafview.java:190) ~[thymeleaf-spring4-2.1.5.release.jar:2.1.5.release] @ org.springframework.web.servlet.dispatcherservlet.render(dispatcherservlet.java:1282) ~[spring-webmvc-4.3.8.build-snapshot.jar:4.3.8.build-snapshot] @ org.springframework.web.servlet.dispatcherservlet.processdispatchresult(dispatcherservlet.java:1037) ~[spring-webmvc-4.3.8.build-snapshot.jar:4.3.8.build-snapshot] @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:980) ~[spring-webmvc-4.3.8.build-snapshot.jar:4.3.8.build-snapshot] @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:897) ~[spring-webmvc-4.3.8.build-snapshot.jar:4.3.8.build-snapshot] @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:970) ~[spring-webmvc-4.3.8.build-snapshot.jar:4.3.8.build-snapshot] 

can tell me how add thymeleaf-layout-dialect correctly?

the issue is:

org.thymeleaf.exceptions.configurationexception: cannot initialize: no template resolvers have been set 

to integrate thymeleaf spring, need configure 3 beans:

  • thymeleafviewresolver bean - set template engine
  • springtemplateengine bean - set template resolver
  • templateresolver bean

in templateengine bean didn't set template resolver, might change templateengine() method following:

@bean public springtemplateengine templateengine(templateresolver templateresolver){     springtemplateengine templateengine  = new springtemplateengine();     templateengine.settemplateresolver(templateresolver);            templateengine.adddialect(new layoutdialect());     return templateengine; } 

spring provide templateresolver bean of springtemplateengine.


btw if define spring-boot-starter-thymeleaf dependency, provide thymeleaf-layout-dialect dependency convenient version, spring use thymeleafautoconfiguration.java - spring boot 1.5.x configure default beans 3 required beans.

for example:

layoutdialect bean define here thymeleafweblayoutconfiguration.thymeleafweblayoutconfiguration():

@configuration @conditionalonclass(name = "nz.net.ultraq.thymeleaf.layoutdialect") protected static class thymeleafweblayoutconfiguration {      @bean     @conditionalonmissingbean     public layoutdialect layoutdialect() {         return new layoutdialect();     }  } 

springtemplateengine bean defined template resolver , dialect here thymeleafweblayoutconfiguration.thymeleafdefaultconfiguration():

@configuration @conditionalonmissingbean(springtemplateengine.class) protected static class thymeleafdefaultconfiguration {    private final collection<itemplateresolver> templateresolvers;    private final collection<idialect> dialects;    public thymeleafdefaultconfiguration(       collection<itemplateresolver> templateresolvers,       objectprovider<collection<idialect>> dialectsprovider) {     this.templateresolvers = templateresolvers;     this.dialects = dialectsprovider.getifavailable();   }    @bean   public springtemplateengine templateengine() {     springtemplateengine engine = new springtemplateengine();     (itemplateresolver templateresolver : this.templateresolvers) {       engine.addtemplateresolver(templateresolver);     }     if (!collectionutils.isempty(this.dialects)) {       (idialect dialect : this.dialects) {         engine.adddialect(dialect);       }     }     return engine;   }  } 

and thymeleafviewresolver bean defined here abstractthymeleafviewresolverconfiguration.thymeleafviewresolver():

@bean @conditionalonmissingbean(name = "thymeleafviewresolver") @conditionalonproperty(name = "spring.thymeleaf.enabled", matchifmissing = true) public thymeleafviewresolver thymeleafviewresolver() {     thymeleafviewresolver resolver = new thymeleafviewresolver();     configuretemplateengine(resolver, this.templateengine);     resolver.setcharacterencoding(this.properties.getencoding().name());     resolver.setcontenttype(appendcharset(this.properties.getcontenttype(),         resolver.getcharacterencoding()));     resolver.setexcludedviewnames(this.properties.getexcludedviewnames());     resolver.setviewnames(this.properties.getviewnames());     // resolver acts fallback resolver (e.g.     // internalresourceviewresolver) needs have low precedence     resolver.setorder(ordered.lowest_precedence - 5);     resolver.setcache(this.properties.iscache());     return resolver; } 

which extended thymeleafautoconfiguration.thymeleaf2viewresolverconfiguration:

@bean @conditionalonmissingbean(name = "thymeleafviewresolver") @conditionalonproperty(name = "spring.thymeleaf.enabled", matchifmissing = true) public thymeleafviewresolver thymeleafviewresolver() {     thymeleafviewresolver resolver = new thymeleafviewresolver();     configuretemplateengine(resolver, this.templateengine);     resolver.setcharacterencoding(this.properties.getencoding().name());     resolver.setcontenttype(appendcharset(this.properties.getcontenttype(),         resolver.getcharacterencoding()));     resolver.setexcludedviewnames(this.properties.getexcludedviewnames());     resolver.setviewnames(this.properties.getviewnames());     // resolver acts fallback resolver (e.g.     // internalresourceviewresolver) needs have low precedence     resolver.setorder(ordered.lowest_precedence - 5);     resolver.setcache(this.properties.iscache());     return resolver; } 

hope clear now.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -