Loading templates from external source

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

Loading templates from external source

mresc
Hi,

is there a posibility to load the template files not from /WEB-INF/ folder, but from another folder on server? Something like /opt/templates or another external source (URL or database)?

Best regards,
Anton.
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

Emanuel
Administrator
Yes, you can load templates from anywhere provided you have the appropriate TemplateResolver for the job.  Thymeleaf comes with 4 template resolvers:
 - ClassLoaderTemplateResolver
 - FileTemplateResolver
 - ServletContextTemplateResolver
 - UrlTemplateResolver

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.

The other template resolvers should be self-explanatory, and I remember someone asking if they could load a template from a database...  *does a search*  Ah, here it is: http://forum.thymeleaf.org/Loading-thymeleaf-template-from-Database-td4025164.html
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

mresc
Hi,

thanks for quick reply. I think FileTemplateResolver is what I need.

Thanks,
Anton.
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

chandanmb
In reply to this post by Emanuel
Hi,
I am beginner in thymeleaf, I know how to use ServletContextTemplateResolver. But i want to know how to use ClassLoaderTemplateResolver so that it can read html files present inside jar files

Thank you
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

Emanuel
Administrator
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();
templateResolver.setPrefix("my-templates.jar/com/example/");
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

chandanmb
Hi Emanuel,

Thanks for the information.. I need little more clarification about where exactly the jar file has to be placed in the project. Can you please elaborate it. So that i can proceed with the project.

Thank you
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

Emanuel
Administrator
With the path examples I gave above, the JAR should be on your class path, which in a web project would be the WEB-INF/lib directory.
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

chandanmb
In reply to this post by Emanuel
Hi Emanuel,

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
o/p: META-INF/
META-INF/MANIFEST.MF
templetes/
templetes/home.html
4.I added this web-inf/lib folder and build that path

5.disptacher-servlet configuration is as shown below

<bean id="classLoaderTemplateResolver"
                class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver ">
                <property name="prefix" value="my-templates.jar/"></property>
                <property name="order" value="1" />
                <property name="templateMode" value="HTML5" />
        </bean>
 
  <bean id="templateEngine"
        class="org.thymeleaf.spring3.SpringTemplateEngine">
    <property name="templateResolver" ref="classLoaderTemplateResolver" />
    <property name="additionalDialects">
        <set>
          <bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
          </set>
          </property>
   
  </bean>

<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine" />
 <property name="order" value="1"></property>

  </bean>

6. I have a welcome-file list index.jsp, from their i am routing to home.html through controller
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

chandanmb
In reply to this post by Emanuel
As i explained above, Can you please elaborate the procedure and rectify my error where i am going wrong. I am getting an exception

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "home", template might not exist or might not be accessible by any of the configured Template Resolvers
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

Emanuel
Administrator
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
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

chandanmb
Hi,
Apart from above configuration, in my controller i am just doing

@RequestMapping(value="/home")
public String getHomePage(){
return "home";
}

Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

Emanuel
Administrator
This post was updated on .
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:

 - prefix: "my-templates.jar/"
 - return value: "home"
 - suffix: (none)

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
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

chandanmb
Hi,
I tried so many combination but still its not able to read from the jar file.

bean id="classLoaderTemplateResolver"
                class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver ">
                <property name="prefix" value="my-templates.jar/templetes/"></property>
                <property name="suffix" value=".html"></property>
                <property name="order" value="1" />
                <property name="templateMode" value="HTML5" />
        </bean>

This is what actually you are telling but still its not able to pick up the html files

Thank you
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

Emanuel
Administrator
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/".
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

chandanmb
Hi,
Before trying that can you please confirm me whether there is a separate jar file for using classloadertemplateresolver. Can you tell me the jar file name?

Thank you
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

Emanuel
Administrator
I can't tell which of 2 questions you're asking:

 - 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.
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

chandanmb
Hi,
Thank you Emanuel. Its working working!!!

Another question is it possible to load css/properties file same as the html files present inside jar?

If it possible? then can you please send me the configuration of dispatcher-servlet

Thank you
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

Emanuel
Administrator
For properties files, Thymeleaf can load properties files for templates so long as they have the same name as the template and are in the same directory (http://www.thymeleaf.org/doc/html/Using-Thymeleaf.html#using-thtext-and-externalizing-text).  That can be configured if you supply an IMessageResolver implementation to the template engine in your Spring configuration.

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.