【问题标题】:Cannot add new tiles fragment [Tiles 2, Spring, Thymeleaf]无法添加新的瓷砖片段 [Tiles 2, Spring, Thymeleaf]
【发布时间】:2015-12-22 16:08:11
【问题描述】:

我一直在我的应用程序中使用 Tiles 2、Thymeleaf 和 Spring MVC。今天我去添加一个新的片段,它似乎没有注册。我仍然得到旧模板。模板解析器似乎忽略了新片段。我试过清理我的项目,但它没有改变任何东西。有谁知道为什么我不能添加这个新的瓷砖片段?

spring-thymeleaf-tiles.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- thymeleaf view resolvers with tiles integration -->
    <bean id="tilesConfigurer"
        class="org.thymeleaf.extras.tiles2.spring4.web.configurer.ThymeleafTilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles/tiles.xml</value>
            </list>
        </property>
    </bean>

    <!-- template resolver -->
    <bean id="templateResolver"
        class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
        <property name="prefix" value="/WEB-INF/tiles/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML5" />
        <property name="characterEncoding" value="utf-8" />
        <property name="cacheable" value="false" />
    </bean>

    <!-- template engine -->
    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
        <property name="additionalDialects">
            <set>
                <bean id="tilesDialect" class="org.thymeleaf.extras.tiles2.dialect.TilesDialect" />
            </set>
        </property>
    </bean>

    <!-- tiles view resolver -->
    <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" />
    </bean>
</beans>

tiles.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN" "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
    <!-- index -->
    <definition name="login" template="templates/login">
        <put-attribute name="head" value="fragments/head :: login_head" />
        <put-attribute name="content" value="content/login :: content" />
        <put-attribute name="privacy_policy" value="fragments/privacy_policy :: privacy_policy" />
        <put-attribute name="scripts" value="fragments/scripts :: loginScripts" />
    </definition>

    <!-- user -->
    <definition name="dashboard" template="templates/dashboard">
        <put-attribute name="head" value="fragments/head :: dashboard_head" />
        <put-attribute name="header" value="fragments/header :: header" />
        <put-attribute name="sidebar" value="fragments/sidebar :: sidebar" />
        <put-attribute name="footer" value="fragments/footer :: footer" />
        <put-attribute name="content" value="content/dashboard :: content" />
        <put-attribute name="privacy_policy" value="fragments/privacy_policy :: privacy_policy" />
        <put-attribute name="scripts" value="fragments/scripts :: dashboardScripts" />
    </definition>

    <definition name="profile" extends="dashboard">
        <put-attribute name="content" value="content/profile :: content" />
    </definition>

    <definition name="assets" extends="dashboard">
        <put-attribute name="content" value="content/assets :: content" />
    </definition>

</tiles-definitions>

login.html(模板)

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:tiles="http://www.thymeleaf.org">
    <th:block tiles:include="head" th:remove="tag" />  
    <body>
        <!-- html -->
        <div id="page">
            <th:block tiles:include="content" th:remove="tag" />
            <th:block tiles:inlcude="privacy_policy" th:remove="tag" />
            <th:block tiles:include="scripts" th:remove="tag" />
        </div>
    </body>
</html>

privacy_policy.html(新片段)

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org"
    xmlns:tiles="http://www.thymeleaf.org">
    <body>
    <th:block tiles:fragment="privacy_policy" th:remove="tag">
        <div class="jumbotron">ASDFASDFASDF</div>
        <h1>ASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDFASDF</h1>
    </th:block>
    </body>
</html>

编辑

我可以通过拼错我的拼贴定义中的属性来强制拼贴错误。

...
    <definition name="login" template="templates/login">
                <put-attribute name="head" value="fragments/head :: login_MISSPELLED" />
...

但是如果我对新片段进行更改,则没有错误

...
    <definition name="login" template="templates/login">
                <put-attribute name="privacy_policy" value="fragments/privacy_policy :: privacy_MISSPELLED" />
...

【问题讨论】:

  • 看起来好像正在缓存某些东西,我为模板解析器设置了可缓存的 false 属性。我不确定这里还会发生什么。
  • 我已经尝试过tomcat清理和清理工作模块目录。我还尝试了 maven clean 项目。两者都没有工作。我在这里错过了什么愚蠢的东西吗?
  • 我尝试废弃整个项目文件夹,撤回所有代码并重新构建,但仍然在这个问题上摸不着头脑。

标签: xml spring spring-mvc thymeleaf tiles


【解决方案1】:

这是一个错字哈哈。哇,那真是太棒了。这是更正。应该注意的是,没有 html 验证,甚至没有来自我的 thymeleaf nature 插件的验证,都认为这是一个问题。我自己的眼睛就在上面闪过:\

login.html(模板)

...
    <th:block tiles:include="privacy_policy" th:remove="tag" />
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    相关资源
    最近更新 更多