Linked Questions

Popular Questions

layout:decorate is not working in thymeleaf

Asked by At

I am trying to introduce a layout(as here) in my application using thymeleaf but couldn't get it working. I have already checked this post.

pom.xml

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.8.RELEASE</version>
        </parent> 
    ...
    <dependency>
        <groupId>nz.net.ultraq.thymeleaf</groupId>
        <artifactId>thymeleaf-layout-dialect</artifactId>
   </dependency>

This is my MvcConfig

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
    ...
    @Bean
    public LayoutDialect layoutDialect() {
        return new LayoutDialect();
    }
}

layout.html as given here. No changes. Added xmlns:th="http://www.thymeleaf.org on checking some article.

<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.thymeleaf.org">
<head>
  <title>Layout page</title>
</head>
<body>
  <header>
    <h1>My website</h1>
  </header>
  <div layout:fragment="content">
    <p>Page content goes here</p>
  </div>
  <footer>
    <p>My footer</p>
    <p layout:fragment="custom-footer">Custom footer here</p>
  </footer>  
</body>
</html>

I'm trying to replace the contents fragment as shown below in login.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.thymeleaf.org"
        layout:decorate="~{layout}">
    <head>
        <title>Application Login</title>
    </head>
    <body>
        <div layout:fragment="content">
            <div th:if="${param.error}">
                Invalid username and password.
            </div>
            <div th:if="${param.logout}">
                You have been logged out.
            </div>
            <form th:action="@{/login}" method="post">
                <div><label> User Name : <input type="text" name="username" value=""/> </label></div>
                <div><label> Password: <input type="password" name="password" value=""/> </label></div>
                <div><input type="submit" value="Sign In"/></div>
            </form>
         </div>   
    </body>
</html>

Am I missing anything?

Related Questions