2

I did a project using Spring with Thymeleaf and Tiles with the following structure :

enter image description here

I have configured my Spring-Servlet :

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

        <bean id="tilesConfigurer" 
                class="org.thymeleaf.extras.tiles2.spring4.web.configurer.ThymeleafTilesConfigurer">
                <property name="definitions">
                        <list>
                                <value>/WEB-INF/tiles-defs.xml</value>
                        </list>
                </property>
        </bean>
        <bean id="tilesViewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
                <property name="viewClass" 
                        value="org.thymeleaf.extras.tiles2.spring4.web.view.ThymeleafTilesView" />
                <property name="templateEngine" ref="templateEngine" />
                <property name="characterEncoding" value="UTF-8" />
        </bean>
        <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
                <property name="templateResolver" ref="templateResolver" />
                <property name="additionalDialects">
                        <set>
                                <bean class="org.thymeleaf.extras.tiles2.dialect.TilesDialect" />
                        </set>
                </property>
        </bean>  

my Tiles Definition is :

<tiles-definitions>
    <definition name="layout" template="layout">
        <put-attribute name="header"   value="header" />
        <put-attribute name="menu"   value="menu" />
        <put-attribute name="footer" value="footer" />
        <put-attribute name="body" />

    </definition>

    <definition name="usersView" extends="layout">
        <put-attribute name="body"   value="pages :: users" />
    </definition>
 </tiles-definitions>

will this give me the following error :

Error resolving template "pages", template might not exist or might not be accessible by any of the configured Template Resolvers 

but when i move the users.html page to the templates folder and make my tiles def as

<definition name="usersView" extends="layout">
        <put-attribute name="body"   value="users" />
    </definition>

its working fine .

So the Question is can Thymeleaf accept My Folder Structure? if Yes ,How?

Dunken
  • 1,311
  • 7
  • 18
  • 30

2 Answers2

3

Yes it can be by using the viewNames property

just change your Spring-Servlet.xml to

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

        </bean>

        <bean id="tilesConfigurer" 
                class="org.thymeleaf.extras.tiles2.spring4.web.configurer.ThymeleafTilesConfigurer">
                <property name="definitions">
                        <list>
                                <value>/WEB-INF/tiles-defs.xml</value>
                        </list>
                </property>
        </bean>
        <bean id="tilesViewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
                <property name="viewClass" 
                        value="org.thymeleaf.extras.tiles2.spring4.web.view.ThymeleafTilesView" />
                <property name="templateEngine" ref="templateEngine" />
                <property name="characterEncoding" value="UTF-8" />
                <property name="order" value="1" />
                <property name="viewNames" value="templates/*,pages/*" />
        </bean>
        <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
                <property name="templateResolver" ref="templateResolver" />
                <property name="additionalDialects">
                        <set>
                                <bean class="org.thymeleaf.extras.tiles2.dialect.TilesDialect" />
                        </set>
                </property>
        </bean> 

and your tiles definition to

<tiles-definitions>
    <definition name="layout" template="templates/layout">
        <put-attribute name="header"   value="templates/header" />
        <put-attribute name="menu"   value="templates/menu" />
        <put-attribute name="footer" value="templates/footer" />
        <put-attribute name="body" />
    </definition>

    <definition name="pages/usersView" extends="layout">
        <put-attribute name="body"   value="pages/users" />
    </definition>
 </tiles-definitions>

Explanation :

we have defined the root folder in the prefix at the Resolver

<property name="prefix" value="/WEB-INF/" />

and extended it by

 <property name="viewNames" value="templates/*,pages/*" />

to the needed folders and any new view folder you create should goes in this property also redirect or forward too

Omar NourdeaN
  • 503
  • 2
  • 7
0

And also make sure that the query written in Repo layer might be not correct. If so please check your query (using CriteriaBuilder, CriteriaQuery, Predicate) once is it satisfying the condition.

Here in my case I tried to getting a value from data base where the condition is not satisfied.

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138