how to override default thymeleaf resolver in spring boot -
hi want override default thymeleaf configuration custom folder of web app. thymeleaf dependencies added pom.xml file. want map web-inf/views/
folder. my current configuration
@bean public thymeleafviewresolver thymeleafviewresolver() { thymeleafviewresolver thymeleafviewresolver = new thymeleafviewresolver(); thymeleafviewresolver.settemplateengine(templateengine()); thymeleafviewresolver.setorder(1); return thymeleafviewresolver; } @bean public springtemplateengine templateengine() { springtemplateengine templateengine = new springtemplateengine(); templateengine.settemplateresolver(templateresolver()); templateengine.adddialect(new layoutdialect()); return templateengine; } @bean public servletcontexttemplateresolver templateresolver() { servletcontexttemplateresolver templateresolver = new servletcontexttemplateresolver(); templateresolver.setprefix("/web-inf/views/"); templateresolver.setsuffix(".html"); templateresolver.settemplatemode("html5"); templateresolver.setorder(1); templateresolver.setcacheable(false); return templateresolver; }
when run app , showing below exceptions.
exception in thread "main" org.springframework.beans.factory.beancreationexception: error creating bean name 'org.springframework.boot.autoconfigure.thymeleaf.thymeleafautoconfiguration$defaulttemplateresolverconfiguration': invocation of init method failed; nested exception java.lang.illegalstateexception: cannot find template location: classpath:/templates/ (please add templates or check thymeleaf configuration) @ org.springframework.beans.factory.annotation.initdestroyannotationbeanpostprocessor.postprocessbeforeinitialization(initdestroyannotationbeanpostprocessor.java:136) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.applybeanpostprocessorsbeforeinitialization(abstractautowirecapablebeanfactory.java:408) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1558) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:539) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:476) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:303) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:299) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:194) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:762) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:757) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:480) @ org.springframework.boot.context.embedded.embeddedwebapplicationcontext.refresh(embeddedwebapplicationcontext.java:118) @ org.springframework.boot.springapplication.refresh(springapplication.java:691) @ org.springframework.boot.springapplication.run(springapplication.java:321) @ org.springframework.boot.springapplication.run(springapplication.java:961) @ org.springframework.boot.springapplication.run(springapplication.java:950) @ com.jo.dsdemo.app.mainapp.main(mainapp.java:17) caused by: java.lang.illegalstateexception: cannot find template location: classpath:/templates/ (please add templates or check thymeleaf configuration) @ org.springframework.util.assert.state(assert.java:385) @ org.springframework.boot.autoconfigure.thymeleaf.thymeleafautoconfiguration$defaulttemplateresolverconfiguration.checktemplatelocationexists(thymeleafautoconfiguration.java:82) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source) @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) @ java.lang.reflect.method.invoke(unknown source) @ org.springframework.beans.factory.annotation.initdestroyannotationbeanpostprocessor$lifecycleelement.invoke(initdestroyannotationbeanpostprocessor.java:349) @ org.springframework.beans.factory.annotation.initdestroyannotationbeanpostprocessor$lifecyclemetadata.invokeinitmethods(initdestroyannotationbeanpostprocessor.java:300) @ org.springframework.beans.factory.annotation.initdestroyannotationbeanpostprocessor.postprocessbeforeinitialization(initdestroyannotationbeanpostprocessor.java:133) ... 17 more
i think simplest way should define spring.thymeleaf.prefix
"/web-inf/templates/"
. otherwise make sure bean has name defaulttemplateresolver
.
untested - helpful. please give feedback, further readers know if it's useful or not.
Comments
Post a Comment