Thymeleaf decorator - how to replace footer from layout by custom footer on one particular page

StackOverflow https://stackoverflow.com/questions/22402372

Pregunta

My question is - I have two files: layout.html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
th:fragment="layout">
<head>
<title >Layout page</title>    
</head>
<body>
<header>
  <h1>My website</h1>
</header>
<section th:include="this :: content">
  <p>Page content goes here</p>
</section>
<footer th:fragment="footer" >
  <p>My footer</p>      
</footer>  
</body>
</html>

And index.html:

<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
th:include="layout :: layout">
<head>
<title>Index page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
 <h2>Hello World!</h2>

<section class="seedstarterlist" th:fragment="content" >
    <form action="#" th:action="@{/add}" th:object="${user}" method="post">
        <table>
            <tbody>
                <tr>
                    <td>name</td>
                    <td><input type="text" th:field="*{name}"  /></td>
                </tr>
                <tr>
                    <td>surname</td>
                    <td><input type="text" th:field="*{surname}"  /></td>
                </tr>
                <tr>
                    <td>email</td>
                    <td><input type="text" th:field="*{email}"  /></td>
                </tr>
                <tr>
                    <td>street</td>
                    <td><input type="text" th:field="*{street}"  /></td>
                </tr>
                <tr>
                    <td>postcode</td>
                    <td><input type="text" th:field="*{postcode}"  /></td>
                </tr>
                <tr>
                    <td>city</td>
                    <td><input type="text" th:field="*{city}"  /></td>
                </tr>
                <tr>
                    <td>district</td>
                    <td><input type="text" th:field="*{district}"  /></td>
                </tr>
                <tr>
                    <td>country</td>
                    <td><input type="text" th:field="*{country}"  /></td>
                </tr>
                <tr>
                    <td>nationality</td>
                    <td><input type="text" th:field="*{nationality}"  /></td>
                </tr>
                <tr>
                    <td>phoneNumber</td>
                    <td><input type="text" th:field="*{phoneNumber}"  /></td>
                </tr>
                <tr>
                    <td>avatar</td>
                    <td><input type="text" th:field="*{avatar}"  /></td>
                </tr>
                <tr>
                    <td>password</td>
                    <td><input type="text" th:field="*{password}"  /></td>
                </tr>
                <tr>
                    <td>status</td>
                    <td><input type="text" th:field="*{status}"  /></td>
                </tr>
                <tr>
                    <td>sex</td>
                    <td><input type="text" th:field="*{sex}"  /></td>
                </tr>
                <tr>
                    <td>birthdate</td>
                    <td><input type="text" th:field="*{birthdate}"  /></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <button type="submit" name="add">Add User</button>
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
</section>
<footer th:fragment="footer" >
  <p>My custom footer</p>      
</footer>

I want to have custom footer only on index.html page. Layout works fine, but I want to override footer, on this particular page, and this is not working.

Any help?

¿Fue útil?

Solución

The answer is that I needed to change my approach and use ThymeLeaf Layout Dialect. I registered additional template engine in Spring configuration files:

<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
  <property name="additionalDialects">
    <set>
      <bean class="nz.net.ultraq.thymeleaf.LayoutDialect"/>
    </set>
  </property>
</bean>

and change declaration on the top of html to:

<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
  layout:decorator="layout.html">

Hope it will help somebody.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top