当前位置: 首页 > 知识库问答 >
问题:

解析模板“页”时出错,模板可能不存在,或者任何已配置的模板解析程序都无法访问模板

盖晋
2023-03-14

我已经配置了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>  

我的瓷砖定义是:

<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>

这会给我以下错误吗:

Error resolving template "pages", template might not exist or might not be accessible by any of the configured Template Resolvers 
<definition name="usersView" extends="layout">
        <put-attribute name="body"   value="users" />
    </definition>

它工作得很好。

所以问题是Thymeleaf能接受我的文件夹结构吗?如果是,怎么做?

共有1个答案

宗政洋
2023-03-14

是的,可以使用viewNames属性

只需将spring-servlet.xml更改为

<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> 

和您的瓷砖定义

<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>
<property name="prefix" value="/WEB-INF/" />
 <property name="viewNames" value="templates/*,pages/*" />
 类似资料: