logo © 2014

Spring Boot Quick Start

This quick start explains how to integrate Jtwig with Spring Boot.

1. Dependencies

Depending on the dependency management mechanism used, adding Jtwig dependencies may differ.

To integrate Jtwig with Spring Boot add the org.jtwig:jtwig-spring-boot-starter:5.85.3.RELEASE dependency. Another important aspect is to add the jcenter repository as Jtwig bundles are stored in bintray.

Gradle

copy
repositories {
    jcenter()
}

dependencies {
    compile "org.jtwig:jtwig-spring-boot-starter:5.85.3.RELEASE"
}

Maven

copy

<repositories>
    <repository>
        <id>jcenter</id>
        <url>https://jcenter.bintray.com/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.jtwig</groupId>
        <artifactId>jtwig-spring-boot-starter</artifactId>
        <version>5.85.3.RELEASE</version>
    </dependency>
</dependencies>

Download

If you don't make use of dependency management systems, it is also possible to download the dependencies via bintray.

2. Jtwig Template

An example of a Jtwig template placed in the classpath, with path index.twig. Note that, Jtwig Spring Boot integration sets the default template location as /templates in the classpath, with files suffixed with .twig.

copy
Hello {{ name }}!

3. Controller Code

An example of a simple Servlet implementation can be seen below.

copy
package org.jtwig.boot.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@EnableAutoConfiguration
public class SampleController {
    @RequestMapping("/{name}")
    public String indexAction (ModelMap model, @PathVariable("name") String name) {
        model.addAttribute("name", name);
        return "index";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}

All the listed code can be found in Jtwig Examples - Gradle Spring Boot Simple.