java - Spring Boot Thymeleaf 3 external javascript template configuration -
i have spring boot application working oauth2 client.
i'm using thymeleaf 3 template engine. these thymeleaf 3 related dependencies in build.gradle.
compile('org.thymeleaf:thymeleaf:3.0.1.release') compile('org.thymeleaf:thymeleaf-spring4:3.0.1.release') compile('nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:2.0.4') compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.1.release')
it makes no difference if reference dependencies this:
ext["thymeleaf.version"] = "3.0.2.release" ext["thymeleaf-layout-dialect.version"] = "2.1.1" dependencies { compile('org.springframework.boot:spring-boot-starter-thymeleaf') }
i want able use thymeleaf 3's html , javascript template modes. html mode works, in javascript mode messagesource not working properly.
here webmvcconfiguration class:
@configuration public class webmvcconfig extends webmvcconfigureradapter implements applicationcontextaware { private static final string character_encoding = "utf-8"; private applicationcontext applicationcontext; @override public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception { this.applicationcontext = applicationcontext; } @bean public viewresolver htmlviewresolver() { thymeleafviewresolver resolver = new thymeleafviewresolver(); resolver.settemplateengine(templateengine(htmltemplateresolver())); resolver.setcontenttype("text/html"); resolver.setcharacterencoding(character_encoding); resolver.setviewnames(new string[] {"*.html"}); return resolver; } @bean public viewresolver javascriptviewresolver() { thymeleafviewresolver resolver = new thymeleafviewresolver(); resolver.settemplateengine(templateengine(javascripttemplateresolver())); resolver.setcontenttype("application/javascript"); resolver.setcharacterencoding(character_encoding); resolver.setviewnames(new string[] {"*.js"}); return resolver; } private templateengine templateengine(itemplateresolver templateresolver) { springtemplateengine engine = new springtemplateengine(); engine.settemplateresolver(templateresolver); return engine; } private itemplateresolver htmltemplateresolver() { springresourcetemplateresolver resolver = new springresourcetemplateresolver(); resolver.setapplicationcontext(applicationcontext); resolver.setprefix("templates/"); resolver.setcacheable(false); resolver.settemplatemode(templatemode.html); resolver.setsuffix(".html"); return resolver; } public itemplateresolver javascripttemplateresolver() { springresourcetemplateresolver resolver = new springresourcetemplateresolver(); resolver.setapplicationcontext(applicationcontext); resolver.setprefix("classpath:/static/js/"); resolver.setcacheable(false); resolver.settemplatemode(templatemode.javascript); // resolver.setsuffix(".js"); return resolver; } }
please note had use "classpath:/static/js/" in javascripttemplateresolver, because when used "static/js/", got following exception:
java.io.filenotfoundexception: not open servletcontext resource [/static/js/headerconfig.js]
also had comment out setsuffix, because following exception:
java.io.filenotfoundexception: class path resource [static/js/typeutils.js.js] cannot opened because not exist
i think indicates main problem can't figure out may cause it.
i have controller handling javascript templates:
@controller public class javascriptcontroller { @requestmapping(method = requestmethod.get, value = "/js/{template}.js") public string jsmapping(@pathvariable("template") string template, model model) { model.addattribute("myattribute", "attribute works!"); return template; } }
my messages.properties files located in
- src/main/resources/messages.properties
- src/main/resources/messages_hu.properties
i have html file in src/main/resources/templates/ folder, references javascript file so:
<script th:src="@{js/typeutils.js}"></script>
the referenced javascript file (js/typeutils.js):
var = [[${myattribute}]]; var b = [[#{test}]]; console.log(a); console.log(b);
when run application , check javascript console, what's being printed out:
attribute works! ??test_hu_hu??
so model attribute passed javascript file, , localization has been detected, message wasn't found 'test'. seems if javascript template mode behaves different html template mode.
how should if fix javascript template mode config in order process messages.properties files, too?
thanks!
i figured out solution (by looking through thymeleaf sourcecode), might not perfect, works!
unlike example given baeldung, following solved problem me:
@configuration public class webmvcconfig implements webmvcconfigurer, applicationcontextaware { private static final string character_encoding = "utf-8"; private applicationcontext applicationcontext; @override public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception { this.applicationcontext = applicationcontext; } @bean public viewresolver htmlviewresolver() { thymeleafviewresolver resolver = new thymeleafviewresolver(); resolver.settemplateengine(templateengine()); resolver.setcontenttype("text/html"); resolver.setcharacterencoding(character_encoding); resolver.setviewnames(new string[] { "*.html" }); return resolver; } @bean public viewresolver javascriptviewresolver() { thymeleafviewresolver resolver = new thymeleafviewresolver(); resolver.settemplateengine(templateengine()); resolver.setcontenttype("application/javascript"); resolver.setcharacterencoding(character_encoding); resolver.setviewnames(new string[] { "*.js" }); return resolver; } @bean public springtemplateengine templateengine() { springtemplateengine engine = new springtemplateengine(); engine.setmessagesource(messagesource()); engine.addtemplateresolver(htmltemplateresolver()); engine.addtemplateresolver(javascripttemplateresolver()); return engine; } private itemplateresolver htmltemplateresolver() { springresourcetemplateresolver resolver = new springresourcetemplateresolver(); resolver.setorder(0); resolver.setcheckexistence(true); resolver.setapplicationcontext(applicationcontext); resolver.setprefix("classpath:templates/"); resolver.setcacheable(false); resolver.settemplatemode(templatemode.html); resolver.setsuffix(".html"); return resolver; } public itemplateresolver javascripttemplateresolver() { springresourcetemplateresolver resolver = new springresourcetemplateresolver(); resolver.setapplicationcontext(applicationcontext); resolver.setorder(1); resolver.setcheckexistence(true); resolver.setprefix("classpath:/static/js/"); resolver.setcacheable(false); resolver.settemplatemode(templatemode.javascript); return resolver; } @bean public messagesource messagesource() { resourcebundlemessagesource msgsource = new resourcebundlemessagesource(); msgsource.setalwaysusemessageformat(false); msgsource.setbasename("messages"); msgsource.setdefaultencoding(character_encoding); msgsource.setfallbacktosystemlocale(true); msgsource.setusecodeasdefaultmessage(false); return msgsource; } }
basically happens set templateengine bean, override thymeleafdefaultconfiguration's default templateengine implementation bean. means each time template needs resolved, same templateengine used sure.
we set order of htmltemplateresolver 0, , order of javascripttemplateresolver 1, try resolve each template first in html template mode, in javascript mode.
it's important, too, set checkexistence flag of springresourcetemplateresolvers true, because way if template can't found, try next templateresolver.
there 1 drawback of solution, try resolve javascript templates unnecessary in html mode first, in javascript mode, there step when resolving javascript resources.
i try solve problem better now, works me.
Comments
Post a Comment