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

别等时光非礼了梦想. 提交于 2019-12-22 13:12:31

问题


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

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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/24260520/error-resolving-template-pages-template-might-not-exist-or-might-not-be-acces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!