Including with passing parameters with Thymeleaf, with Spring MVC.

Looks easy.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>biz.tugay</groupId>
    <artifactId>smvcpoc</artifactId>
 
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
 
    <name>smvcpoc Maven Webapp</name>
    <url>http://maven.apache.org</url>
 
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.0.RELEASE</version>
            <scope>compile</scope>
        </dependency>
 
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring4</artifactId>
            <version>3.0.0.RELEASE</version>
        </dependency>
    </dependencies>
 
    <build>
        <finalName>smvcpoc</finalName>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.1.v20140609</version>
                <configuration>
                    <scanIntervalSeconds>2</scanIntervalSeconds>
                    <webApp>
                        <contextPath>/</contextPath>
                    </webApp>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
 
    <servlet>
        <servlet-name>springDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servletContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>springDispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
 
</web-app>

servlet-context.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-4.0.xsd">
 
    <bean id="homeController" class="biz.tugay.HomeController" />
 
    <bean id="templateResolver"
          class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
        <constructor-arg ref="servletContext"/>
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML5" />
    </bean>
 
    <bean id="templateEngine"
          class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
    </bean>
 
    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
    </bean>
 
</beans>

Message.java
package biz.tugay;
 
/* User: koray@tugay.biz Date: 2016/06/12 */
public interface Message {
    String getMessageBody();
}

SuccessMessage.java
package biz.tugay;
/* User: koray@tugay.biz Date: 2016/06/12 */
public class SuccessMessage implements Message {
    public String getMessageBody() {
        return "This is the success message!";
    }
}

ErrorMessage.java
package biz.tugay;
/* User: koray@tugay.biz Date: 2016/06/12 */
public class ErrorMessage implements Message {
    public String getMessageBody() {
        return "This is the error message!";
    }
}

HomeController.java
package biz.tugay;
 
/* User: koray@tugay.biz Date: 2016/06/12 */
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
@Controller
@RequestMapping(value = "/")
public class HomeController {
    @RequestMapping(method = RequestMethod.GET)
    public String foo(Model model) {
        model.addAttribute("successMessage", new SuccessMessage());
        model.addAttribute("errorMessage", new ErrorMessage());
        return "index";
    }
}

messageFile.html
<!DOCTYPE html>
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Repid Type POC</title>
</head>
<body>
<!--@thymesVar id="messageObject" type="biz.tugay.Message"-->
<div th:fragment="messageDiv(messageObject)">
    <span th:text="${messageObject.getMessageBody()}"></span>
</div>
</body>
</html>

index.html
<!DOCTYPE html>
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Repid Type POC</title>
</head>
<body>
<!--@thymesVar id="successMessage" type="biz.tugay.Message"-->
<!--@thymesVar id="errorMessage" type="biz.tugay.Message"-->
<div th:include="messageFile :: messageDiv(${successMessage})"></div>
<div th:include="messageFile :: messageDiv(${errorMessage})"></div>
</body>
</html>