Include header/footer/banner/externalURL

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

Include header/footer/banner/externalURL

mazzi
Hello,

I'm building a page with Thymeleaf that has a dynamic banner (not strictly an image) that is hosted in another application server.

It's a way to include the content in a Thymeleaf page with a tag?

Is not useful to use <div th:include="x">  because x is an URL.

Thanks in advance.
Reply | Threaded
Open this post in threaded view
|

Re: Include header/footer/banner/externalURL

danielfernandez
Administrator
You can use th:include.

You have add a new UrlTemplateResolver, besides the template resolver you already have, with a lower priority (set its "order" priority to a higher value than your current template resolver).

That "UrlTemplateResolver" will allow you to specify template names as URLs. Including remote ones. So now you will be able to do:

<div th:include="http://banner.server.com/myBanner.html">

...but that would include the whole "myBanner.html" page, which you probably don't want. In order to avoid this, your first option would need that the myBanner.html page you are obtaining had a "th:fragment" declaration:

<div th:include="http://banner.server.com/myBanner.html :: fragmentName">

But this is very unlikely. So a much better option would be to apply a DOM selector (very similar to XPath) to that page. DOM selectors are specified between brackets instead of a fragment name.

For example, if you banner was contained in myBanner.html in a "div" with id="banner", you could do:

<div th:include="http://banner.server.com/myBanner.html :: [//div[@id='banner']]">


Hope this helps.

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

Re: Include header/footer/banner/externalURL

danielfernandez
Administrator

Forgot to mention: that URL can also be an expression:

<div th:include="${myBannerServerUrl} :: [//div[@id='banner']]">
Reply | Threaded
Open this post in threaded view
|

Re: Include header/footer/banner/externalURL

mazzi
In reply to this post by danielfernandez
Thanks for the reply!

I configured spring correctly to add the correct resolvers in the correct priority and I must say that yes, I wish to include the whole URL as you mention in the first example :D

The error that I got now is

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "http://banner.server.com/myBanner.html", template might not exist or might not be accessible by any of the configured Template Resolvers (mypage:27)

Of course that the URL is valid and existent.

When I invoke my page, the logger info is:
[THYMELEAF] * Template resolvers (in order):
[THYMELEAF]     * [10] org.thymeleaf.templateresolver.ServletContextTemplateResolver
[THYMELEAF]     * [20] org.thymeleaf.templateresolver.UrlTemplateResolver

So is well configured (I think)

Any ideas?

Thanks in advance!
Reply | Threaded
Open this post in threaded view
|

Re: Include header/footer/banner/externalURL

danielfernandez
Administrator
Could you please post the configuration of your UrlTemplateResolver bean at the Spring XML configuration file?

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

Re: Include header/footer/banner/externalURL

mazzi
Here it is.

        <bean id="templateResolver"
                class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
                <property name="prefix" value="/WEB-INF/pages/" />
                <property name="suffix" value=".html" />
                <property name="templateMode" value="HTML5" />
                <property name="order" value="10" />
        </bean>
       
        <bean id="urlTemplateResolver"
                class="org.thymeleaf.templateresolver.UrlTemplateResolver">
                <property name="order" value="20" />                       
        </bean>

        <bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
                <property name="templateResolvers">
                        <list>
                                <ref bean="templateResolver"/>
                                <ref bean="urlTemplateResolver"/>
                        </list>
                </property>
                <property name="messageResolver"  ref="messageResolver" />
        </bean>



I must configure prefix, suffix and template mode in both?

Reply | Threaded
Open this post in threaded view
|

Re: Include header/footer/banner/externalURL

mazzi
Seems to be an Unknown Host exception. Maybe a proxy issue.
If I found the solution I let you know.

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

Re: Include header/footer/banner/externalURL

mazzi
Well, thanks for the support.

I made an custom TemplateResolver that makes a connection through a configurable proxy in spring and it's working.

One of the problems was the proxy that we use.
The other was the way that I was using th:include.

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

Re: Include header/footer/banner/externalURL

supun
Just in case if you need java configuration, here it is.
             
    @Bean public ThymeleafViewResolver thymeleafViewResolver()
    {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine( templateEngine() );
        return viewResolver;
    }

    @Bean public SpringTemplateEngine templateEngine()
    {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        Set<ITemplateResolver> templateResolvers = new HashSet();
        templateResolvers.add( thymeleafTemplateResolver() );
        templateResolvers.add( urlTemplateResolver() );
        templateEngine.setTemplateResolvers( templateResolvers );

        return templateEngine;
    }

    @Bean public SpringResourceTemplateResolver thymeleafTemplateResolver()
    {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setPrefix( "/WEB-INF/static/" );
        templateResolver.setSuffix( ".html" );
        templateResolver.setTemplateMode( "HTML5" );
        templateResolver.setOrder( 2 );
        return templateResolver;
    }

     @Bean public UrlTemplateResolver urlTemplateResolver()
    {
        UrlTemplateResolver urlTemplateResolver = new UrlTemplateResolver();
        urlTemplateResolver.setSuffix( ".html" );
        urlTemplateResolver.setCharacterEncoding( "UTF-8" );
        urlTemplateResolver.setTemplateMode( "HTML5" );
        urlTemplateResolver.setCacheable( false );
        urlTemplateResolver.setOrder( 3 );
        return urlTemplateResolver;
    }