Render template which is from InputStream

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

Render template which is from InputStream

smallfish
Is there any way to render a template  which is from an InputStream object?
How could I do?
Reply | Threaded
Open this post in threaded view
|

Re: Render template which is from InputStream

danielfernandez
Administrator
Currently Thymeleaf offers you five ITemplateResolver implementations, each one of them obtaining an InputStream (which is what Thymeleaf uses internally) for the template in a different way:

1. ClassLoaderTemplateResolver obtains the input stream from a classloader resource (e.g. a file in a .jar or in a file in the classpath).

2. ServletContextTemplateResolver obtains the input stream from a servlet context resource.

3. FileTemplateResolver obtains the input stream from a File object, anywhere in the file system.

4. UrlTemplateResolver obtains the input stream from a URL.

5. TemplateResolver is a generic implementation, allowing you to specify your own IResourceResolver implementation that obtains inputstreams in whichever way you need.

If none of the first four options suits you, you can create your own resource resolver (implementing IResourceResolver) and set it to a TemplateResolver instance.

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: Render template which is from InputStream

smallfish
But how do I configure a classLoaderTemplateResolver?
Here is  a fragment of the onfiguration file.
        <bean id="servletContextTemplateResolver"
                class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
                <property name="prefix" value="/WEB-INF/views/" />
                <property name="order" value="1" />
                <property name="suffix" value=".html" />
                <property name="templateMode" value="HTML5" />
        </bean>

        <bean id="classLoaderTemplateResolver"
                class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver ">
                <property name="order" value="2" />
                <property name="templateMode" value="HTML5" />
        </bean>
        <bean id="templateEngine" class="org.thymeleaf.spring3.SpringThymeleafTemplateEngine">
                <property name="templateResolvers">
                        <set>
                                <ref bean="servletContextTemplateResolver" />
                                <ref bean="classLoaderTemplateResolver" />
                        </set>

                </property>

        </bean>
And a template file which name is 'createForm.html'  is in 'org.crusoe' classpath.
I got an exception:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "org.crusoe.createForm.html", 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: Render template which is from InputStream

danielfernandez
Administrator
Hi,

Classloader resources in Java do not use the package notation "." for nesting, but the folder notation "/".

So, if your template is in the "org.crusoe" package and is called "createForm.html", you should try to resolve it as "org/crusoe/createForm.html".

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: Render template which is from InputStream

chandanmb
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