Quantcast

Themes in Thymeleaf

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Themes in Thymeleaf

Reza
Does the framework provide any solution for themes throughout a website? I tried to use spring mvc theme solution with thymeleaf but I was unsuccessful and I am getting parsing errors.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Themes in Thymeleaf

CWayman
I've started using sitemesh3 with thymeleaf and I'm pleased with it to add a common theme to my pages.

I have separate HTML pages (thymeleaf fragments) for headers, footers and sidebars and then sitemesh wraps any templates I have with the sitemesh decorator.

Remember sitemesh uses a full URL to your decorator so it will have to be "WEB-INF/templates/mydecorator.html" in the config file, this caught me out initially. (http://www.sitemesh.org/)

I am at work now so I don't have any examples but I should be able to provide something later on if you would like.

Craig.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Themes in Thymeleaf

Reza
Thanks Mate. I will give it a try. Can you please provide a sample ?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Themes in Thymeleaf

Reza
In reply to this post by CWayman
Havent been successful. It just doesnt pick up my decorator. :(
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Themes in Thymeleaf

Reza
Fixed it. There is a problem when you have a decorator with html extension. I dont know why but when I set it as jsp it works. Unfortunately there is not much documentation for thymeleaf and sitemesh in their websites or in the internet.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Themes in Thymeleaf

CWayman
In reply to this post by Reza
Hi Reza,

Sorry for the late reply, I didn't get notified of your responses:

in my /WEB-INF/sitemesh3.xml file I have:

<?xml version="1.0" encoding="UTF-8"?>
<sitemesh>

        <mapping path="/*" decorator="/WEB-INF/templates/decorators/master.html"/>
       
</sitemesh>

In my web.xml I have the following for the sitemesh servlet:

    <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

I forgot to mention I was also using spring so In my web.xml I also have:

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>dispatcher</welcome-file>
    </welcome-file-list>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

 and in my dispatcher-servlet.xml I have:

       
    <mvc:view-controller path="/WEB-INF/templates/decorators/master.html" view-name="decorators/master" />

       
    <mvc:view-controller path="/" view-name="index" />

and in my spring applicationContext.xml

        <bean id="templateResolver"
                class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
                <property name="prefix" value="/WEB-INF/templates/" />
                <property name="suffix" value=".html" />
                <property name="templateMode" value="HTML5" />
        </bean>

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

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

I think this should be enough to get started, I think spring and sitemesh filters intercept the call first, goes to the dispatcher which then forwards onto the sitemesh configuration and then loads the index page decorated by sitemesh, it took me a while to get working.

I will try and cut down my app into a little example for you soon (and I'll subscribe to the thread for updates!)

Craig.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Themes in Thymeleaf

CWayman
In reply to this post by Reza
this shouldn't be the case, sitemesh should decorate any html file, apparently it doesn't need JSPs to work.  I've posted some of my code for the time being.  Hopefully it will help.

Craig.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Themes in Thymeleaf

Reza
Thanks CWayman. I am using the older version of sitemesh so my config looks slightly different. As soon as I change the extension of layout/decorator file to html my pages become blank (empty response) without any exception/error.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Themes in Thymeleaf

Alex
In reply to this post by CWayman
Hi Craig,

Are you using sitemesh 2 or 3?

And if you are using Maven2, can you help post your POM dependency config for sitemesh?

Thanks in advance.

Alex
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Themes in Thymeleaf

CWayman
Hi Alex, Reza,

I have cut down my app and created a sitemesh3 and thymeleaf example for you.

I am using google app engine to play around so there are some xml files for that in there too.

The app is all mavenized so in order to run it locally you should just need to do "mvn clean package gae:run" and then point your browser to http://localhost:8080 to see it all in action.  It should suck down all the dependencies if you don't have them.

It's all very minimal just to show the decorator working while navigating between two pages  but should be easily extendable using the thymeleaf-spring3 documentation on the site.

Hope you find it useful.

As these go I accept no responsibility for any consequences of using this demo blah blah blah, unless it's world peace, then I want all the credit! :)

Let me know what you think.

Craig.

thymeleaf-sitemesh3.zip
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Themes in Thymeleaf

Alex
Hi Craig,

Thanks a lot. The credit belongs to you already

Alex
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Themes in Thymeleaf

danielfernandez
Administrator
In reply to this post by CWayman
Fantastic work, Craig. Thanks so much.

What about turning this app into a how-to for the Thymeleaf website? I'll reach you by email.

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Themes in Thymeleaf

Reza
In reply to this post by CWayman
Great job. But I thought we werent supposed to use sitemesh 3 for production so I was trying to use the stable version.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Themes in Thymeleaf

CWayman
With all the bits of the app in place currently it should be much work convert it to use sitemesh2.  Maybe just an hours work.

I think you would need to change the package/name of the servlet used in the web.xml file.

rename the sitemesh3.xml file to sitemesh.xml

change the content into sitemesh 2 compliant configuration, but as we only have a reference to one decorator I think this would be simple.

and then change the tags in the decorator to be the V2 style.

and change the dependencies in the pom.xml

The spring configuration would be identical I believe.

I do agree for production systems you should probably use sitemesh2, the website does say as much, and it's probably something to be considered.

I am going to write the app up as a tutorial to be hosted on the thymeleaf site and I will include examples of both sitemesh2 and 3.  It may be a little way away though as I am off on holiday from next Friday for two weeks.

Until then happy coding!
Craig.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Themes in Thymeleaf

Reza
In reply to this post by CWayman
Thanks again. It works with sitemap3 ... I need to hope for it to be released sooner.. It just doesnt work with the stable version.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Themes in Thymeleaf

CWayman
In reply to this post by CWayman
So I think I may have been a little presumptuous there about sitemesh2.  I decided to have a brief look before going to bed.

By the looks of things sitemesh2 uses a JSP tag library in order to do its processing meaning the decorators files will need to be JSPs.

If you can live without timeleaf tags in your decorator JSP page and just in the html files it includes then this should work for you.  As such I've had to use JSP includes rather than thymeleaf fragments to import the header and footer.

It looks like you may need to create custom template resolvers (mentioned in the documentation) so that thymeleaf will also parse and convert any th attributes inside the JSP page too.  The spring config for thymeleaf is successfully converting the html pages (because the suffix is html) but when viewing the source of the JSP page the th attributes were still present.

Again, hope this helps!

thymeleaf-sitemesh2.zip

Craig.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Themes in Thymeleaf

Reza
I am not as experienced as you in java web world but that is why I was having problem with thymeleaf and sitemesh 2. I am now using sitemesh 3 and hoping it is stable enough.

Thanks for your replies and the samples again. You are doing a good job :)
Loading...