Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.mcjp78:spark-template-thymeleaf:0.0.1'
}
dependencies {
implementation("com.github.mcjp78:spark-template-thymeleaf:0.0.1")
}
<dependency>
<groupId>com.github.mcjp78</groupId>
<artifactId>spark-template-thymeleaf</artifactId>
<version>0.0.1</version>
</dependency>
libraryDependencies += "com.github.mcjp78" % "spark-template-thymeleaf" % "0.0.1"
:dependencies [[com.github.mcjp78/spark-template-thymeleaf "0.0.1"]]
Note: By default, spark-template-thymeleaf expects all templates to be under META-INF/templates, to be valid HTML5 (otherwise an exception is thrown during rendering) and have .html as the file suffix. So the path for the template in this example would be /META-INF/templates/testpage.html
How to use the Thymeleaf template route for Spark example:
package spark.template.thymeleaf.example;
import static spark.Spark.get;
import java.util.HashMap;
import java.util.Map;
import spark.ModelAndView;
import spark.Request;
import spark.Response;
import spark.TemplateViewRoute;
import spark.template.thymeleaf.ThymeleafTemplateEngine;
public class ThymeleafExample {
public static class Author {
private String name;
public Author(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static class TestViewRoute implements TemplateViewRoute {
public ModelAndView handle(Request request, Response response) {
Map<String, Object> model = new HashMap<String, Object>();
model.put("title", "testpage");
model.put("author", new Author(
"Markus W Mahlberg <markus.mahlberg@icloud.com"));
return new ModelAndView(model, "testpage");
}
};
public static void main(String[] args) {
get("/hello", new TestViewRoute(), new ThymeleafTemplateEngine());
}
}