Yes, you can load templates from anywhere provided you have the appropriate TemplateResolver for the job. Thymeleaf comes with 4 template resolvers:
Most of the examples you've seen probably use the ServletContextTemplateResolver, which can load anything from the context root of your web application. I've used it to load stuff from the main web content directory instead of WEB-INF - just a personal preference.
So if you've got your ServletContextTemplateResolver code working, replace instances of it to use the ClassLoaderTemplateResolver instead, then when you refer to your templates, you will need to find out what path to give so that you can get at the HTML files inside the JARs. eg:
- if the JAR is called my-templates.jar, and my-template.html is in the default package, the path to get to it would be "my-templates.jar/my-template.html", or "/my-template.html".
- if the JAR is instead inside a package, say com.example, the path to get to it would be "my-templates.jar/com/example/my-template.html", or "/com/example/my-template.html".
Once you've figure out the path common to all your templates, you might want to set it as a suffix to the template resolver to save you having to write that part out over and over again:
TemplateResolver templateResolver = new ClassLoaderTemplateResolver();
Sorry i'm not getting the procedure. I will explain how i build the project one by one
1.Template folder actually contains multiple html files,presently it contains only 1 html file called home.html
2.I created jar of that folder by command jar cf my-templates.jar Template
3.Jar file contains jar tf my-templates.jar
4.I added this web-inf/lib folder and build that path
5.disptacher-servlet configuration is as shown below
In your controller, when you're rerouting to home.html, what string is that reroute method returning? If it's just 'home.html', then given your configuration, the ClassLoaderTemplateResolver will look for: my-templates.jar/home.html
You'll need to add a 'templetes/' to either the prefix property, or the string being returned by your controller method so that it points to: my-templates.jar/templetes/home.html
The path given to a template resolver is: prefix + method return value + suffix. The path you want is "my-templates.jar/templetes/home.html", or "/templetes/home/html". However, given your code and configuration:
This will give the ClassLoaderTemplateResolver the path of "my-templates.jar/home", which is not where your template is.
So you've got to make all those components line up so that you end-up asking the ClassLoaderTemplateResolver for the right path:
- one way might be to change the return value to "templetes/home.html"
- another might be to update the prefix to be "my-templates.jar/templetes/" and add a ".html" suffix property to your ClassLoaderTemplateResolver bean configuration
I may have been wrong about including the JAR in the path. I just put together an example app which would produce paths that started with "test.jar" (that was the name of my JAR file, inside was a template under "/test/Test.html") and they didn't work. When I switched it to start with a / and excluded the JAR (so my paths were "/test/Test.html"), that worked.
Could you try that? Remove the "my-templates.jar" part from the prefix property, so the prefix becomes "/templetes/".
- If you're asking if the template I loaded, test.html, was in a JAR file: yes, it was. The JAR was called test.jar
- If you're asking if ClassLoaderTemplateResolver requires additional dependencies/JARs to work: no, it doesn't, it's part of Thymeleaf.
I don't know about other file types. You could point the template resolver to a CSS file, but Thymeleaf then wouldn't know what to do with it, or might even throw an exception if it tries to parse a CSS file as a template.
I haven't done either of those things, so don't have any code to share with you, sorry.
I am working on a Maven based Project and the project create a Jar file , so i want to access this jar file in another Thymeleaf based project. Is this possible with ClassLoaderTemplateResolver? and how to config this ClassLoaderTemplateResolver ? using a class in springboot or just to add a config.xml in the source folder?
So long as the JAR file is in the classpath, then you should be able to access any templates inside it using the ClassLoaderTemplateResolver like you would if the templates were in the same project. If the JAR isn't in the classpath, then you might need to implement your own ITemplateResolver that can peek into JAR files and pull out the template.